Home > Article > Web Front-end > How to convert js json string to json object
How to convert js json string to json object: 1. Use "$.parseJSON( jsonstr );" to convert json string into json object; 2. Use "eval('(' jsonstr ')' );"Convert json string into json object and so on.
Recommended: "js video tutorial"
How to convert Json string in js into Json object (4 types Conversion method)
Conversion methods supported by the browser (Firefox, chrome, Opera, safari, ie9, ie8) and other browsers:
JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串
$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象
JSON official 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 at https://github.com/douglascrockford/JSON-js. Generally, json2.js is used now.
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 a string into a JSON object, but these methods are not recommended. This method is unsafe and eval will execute the expression in the json string.
1>Conversion methods supported by jQuery plug-in:
The code is as follows:
$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象
2>Conversion methods supported by browsers (Firefox, chrome, opera, safari, ie9, ie8) and other browsers:
The code is as follows:
JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串
Note: ie8 (compatibility mode), ie7 and ie6 do not have JSON objects. It is recommended to use the official JSON method and 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 small characters outside the json character. Brackets
Note: ie8 (compatibility mode), ie7 and ie6 can also use eval() to convert a string into a JSON object, but these methods are not recommended. This method is unsafe and eval will execute the expression in the json string.
4>JSON official conversion method:
http://www.json.org/ provides a json.js so that ie8 (compatibility mode), ie7 and ie6 can support JSON objects and Its stringify() and parse() methods;
You can get this js at https://github.com/douglascrockford/JSON-js, generally json2.js is used now.
The above is the detailed content of How to convert js json string to json object. For more information, please follow other related articles on the PHP Chinese website!