Home  >  Article  >  Web Front-end  >  How to convert js json string to json object

How to convert js json string to json object

藏色散人
藏色散人Original
2020-10-23 10:32:5012755browse

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.

How to convert js json string to json object

Recommended: "js video tutorial"

How to convert Json string in js into Json object (4 types Conversion method)

1. Summary

One sentence summary: Native method (the method supported by the browser by default)

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对符串

1. What is the method for jquery to convert a json string into a json object?

$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象

2. What is the official json conversion method - how to convert Json string in js into Json object?

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.

3. Conversion methods supported by Javascript - How to convert Json string in js into Json object?

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.

2. Conversion between Json objects and Json strings in js (4 conversion methods)

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!

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