Convert JS object to JSON string

后端 未结 27 2496
庸人自扰
庸人自扰 2020-11-22 00:43

If I defined an object in JS with:

var j={\"name\":\"binchen\"};

How can I convert the object to JSON? The output string should be:

27条回答
  •  情歌与酒
    2020-11-22 01:38

    if you have a json string and it's not wrapped with [] then wrap it up first

    var str = '{"city": "Tampa", "state": "Florida"}, {"city": "Charlotte", "state": "North Carolina"}';
    str = '[' + str + ']';
    var jsonobj = $.parseJSON(str);
    

    OR

    var jsonobj = eval('(' + str + ')');
    console.log(jsonobj);
    

提交回复
热议问题