Home > Article > Web Front-end > How to convert json string with javascript
Method: 1. Use parseJSON method, syntax "$.parseJSON(string)"; 2. Use JSON.parse method, syntax "JSON.stringify(string)"; 3. Use eval method, Syntax "eval('(' string')')".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
1. Conversion methods supported by jQuery plug-in:
Example:
$.parseJSON(jsonstr); //jQuery.parseJSON(jsonstr), you can Convert json string to json object
Example:
JSON.parse(jsonstr); //You can convert json strings into json objects
JSON.stringify(jsonobj); //You can convert json objects into json strings
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.
eval('(' jsonstr ')'); //You can convert a json string into a json object. Note that it needs to be wrapped outside the json character. A pair of parentheses
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 json expression in a string.
http://www.json.org/ provides a json.js, so that ie8 (compatibility mode), ie7 and ie6 can support it JSON object and its stringify() and parse() methods;
You can get this js at https://github.com/douglascrockford/JSON-js, generally json2.js is used now.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to convert json string with javascript. For more information, please follow other related articles on the PHP Chinese website!