Home > Web Front-end > JS Tutorial > Conversion between Json objects and Json strings (4 conversion methods)_json

Conversion between Json objects and Json strings (4 conversion methods)_json

WBOY
Release: 2016-05-16 17:39:17
Original
1326 people have browsed it
1> Conversion methods supported by jQuery plug-in:
Copy code The code is as follows:

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


2> Conversion supported by browser Method (Firefox, chrome, opera, safari, ie9, ie8) and other browsers:
Copy code The code is as follows:

JSON.parse(jsonstr); //You can convert a json string into a json object
JSON.stringify(jsonobj); //You can convert a json object into a json string

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 json string into json object, please note that it needs to be in json The character is wrapped in a pair of parentheses
Note: ie8 (compatibility mode), ie7 and ie6 can also use eval() to convert the string into a JSON object, but these methods are not recommended. This method is unsafe and eval will execute json expression in a string.

4>JSON official conversion method:
http://www.json.org/ provides a json.js, such as ie8 (compatibility mode), ie7 and ie6 It 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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template