Home  >  Article  >  Web Front-end  >  How to convert json string with javascript

How to convert json string with javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-11 17:34:292012browse

Method: 1. Use parseJSON method, syntax "$.parseJSON(string)"; 2. Use JSON.parse method, syntax "JSON.stringify(string)"; 3. Use eval method, Syntax "eval('(' string')')".

How to convert json string with javascript

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

2. Conversion methods supported by browsers (Firefox, chrome, opera, safari, ie) and other browsers:

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.

3. Conversion methods supported by Javascript:

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.

4. JSON official conversion method:

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!

Statement:
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