Home >Web Front-end >JS Tutorial >How to convert javascript json string into object

How to convert javascript json string into object

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-08 18:26:4224176browse

Conversion method: 1. Use the "JSON.parse()" method, the syntax is "JSON.parse (valid JSON string)"; 2. Use the "JSON.stringify()" method, the syntax is "JSON. stringify(valid JSON string)".

How to convert javascript json string into object

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

In actual projects, we often encounter character format problems. Write them down for easy viewing in the future. Two functions are used: JSON.stringify() and JSON.parse().

Use ajax to request data from the background, and the background returns data to the front end. Obviously the back-end script writes the json format processed by the json function, but when the front-end receives the data, it is sometimes an object and sometimes a string. It’s puzzling. The specific reason is not clear to me yet.

During the data transmission process, json is passed in the form of text, that is, a string, and JS operates on JSON objects. Therefore, the key is to convert between JSON objects and JSON strings.

For example:

JSON string:

var str1 = '{ "name": "cxh", "sex": "man" }';

JSON object:

var str2 = { "name": "cxh", "sex": "man" };

1. Convert JSON string to JSON object

To use str1 above, you must use the following method to convert it to JSON object first:

//Convert from JSON string to JSON Object

var obj = eval('(' + str + ')');

or

var obj = str.parseJSON(); //由JSON字符串转换为JSON对象

or

var obj = JSON.parse(str); //由JSON字符串转换为JSON对象

Then, you can read it like this:

Alert(obj.name);
Alert(obj.sex);

Special note: If obj is originally a JSON object, it will still be a JSON object after conversion using the eval() function (even if it is converted multiple times), but there will be problems (throwing a syntax exception) after using the parseJSON() function.

2. You can use toJSONString() or the global method JSON.stringify() to convert the JSON object into a JSON string.

For example:

var last=obj.toJSONString(); //将JSON对象转化为JSON字符

or

var last=JSON.stringify(obj); //将JSON对象转化为JSON字符
alert(last);

Note:

Among the above methods, except for the eval() function that comes with js, the other methods all come from the json.js package. The new version of JSON modifies the API and injects both JSON.stringify() and JSON.parse() methods into the built-in objects of Javascript. The former becomes Object.toJSONString(), and the latter becomes String. parseJSON(). If you are prompted that the toJSONString() and parseJSON() methods cannot be found, it means that your json package version is too low.

[Recommended learning: javascript video tutorial]

The above is the detailed content of How to convert javascript json string into 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