Converting json json string object analytically

eg: JSON string:

var str1 = '{ "name": "cxh", "sex": "man" }'; 

JSON object:

var str2 = { "name": "cxh", "sex": "man" };

A, JSON string into a JSON object

To use the above str1, you use the following method to be converted to JSON object:

// string into a JSON object JSON

var obj = eval ( '(' + str + ')'); // write the data format

and/or

var obj = str.parseJSON (); // Converts a string to a JSON object JSON

and/or

var obj = JSON.parse (str); // Converts a string to a JSON object JSON

Then, you can read this:

Alert(obj.name);

Alert(obj.sex);

Special Note: If obj has always been a JSON object, then use eval () function after conversion (even multiple conversions) or JSON object, but using parseJSON () function will have problems after treatment (grammar throws an exception).

Second, you can use the toJSONString () method of the JSON.stringify or global () object into a JSON JSON string.

E.g:

var last = obj.toJSONString (); // JSON object into the character JSON

or

var last = JSON.stringify (obj); // JSON object into the character JSON

alert(last);

Guess you like

Origin blog.csdn.net/weixin_33755847/article/details/91021544