Convert javascript object to json string

1> Conversion methods supported by the jQuery plugin

Copy the code The code is as follows:

$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr), can convert json string into json object (javascript object)



2> Conversion methods supported by browsers (Firefox, chrome, opera, safari, ie9, ie8) and other browsers: 

Copy the code The code is as follows:
This is commonly used
JSON.parse(jsonstr); //You can convert json strings into json objects 
JSON.stringify(jsonobj); //You can convert json objects (javascript objects, or arrays) into json pairs 
var sss = [{code: 'tdsq', items: [{code: 'fpjcbg', caption: 'not enough'}]}]  


Note: ie8 (compatibility mode), ie7 and ie6 do not have JSON objects. It is recommended to use the official JSON method to introduce json.js. 

3> Conversion methods supported by Javascript
eval('(' + jsonstr + ')'); //You can convert a json string into a json object, note that you need to wrap a pair of parentheses around the json character 
Note: ie8 (compatibility mode ), ie7 and ie6 can also use eval() to convert strings to JSON objects, but these methods are not recommended. This method is unsafe and eval will execute expressions in JSON strings. 

4> The official JSON conversion method
http://www.json.org/ provides a json.js, so that ie8 (compatibility mode), ie7 and ie6 can support JSON objects and their stringify() and parse() methods ; 
You can get this js on https://github.com/douglascrockford/JSON-js, generally json2.js is used now.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780895&siteId=291194637