Conversion between json string and json object-learn you will be invincible!

Posted May 27, 20201 min read

1. The use of jq plugin parseJSON()

$.parseJSON(jsonstr); //jq method. json string to json object

var str1 = '{"name":"cxh", "sex":"man"}';
var obj = $. parseJSON(str1)
console.log(obj); //{name:"cxh", sex:"man"}

2. Browser conversion methods(Firefox, chrome, opera, safari, ie) and other browsers:

JSON.parse(jsonstr) //json string to json object
JSON.stringify(jsonobj); //JSON object to json string

var str1 = '{"name":"cxh", "sex":"man"}';
var obj = JSON.parse(str1)
console.log(obj); //{name:"cxh", sex:"man"}


var obj2 = JSON.stringify(obj2);
console.log(obj); //'{"name":"cxh", "sex":"man"}'

3. js comes with eval function, which needs to add parentheses eval('(' + str + ')');

eval('(' + jsonstr + ')'); //json string to json object

var str1 = '{"name":"cxh", "sex":"man"}';
var obj = eval("(" + str1 + ")")
console.log(obj); //{name:"cxh", sex:"man"}

Editor is not easy, if there is any harvest, WeChat rewards editor to drink Wahaha
image.png

Single dog's sunflower collection, a must-have for sisters, stay tuned!
image.png