Home > Web Front-end > JS Tutorial > body text

How to convert json string to json object in js

青灯夜游
Release: 2019-03-28 10:24:27
forward
7015 people have browsed it

The content of this article is to introduce the method of converting json strings into json objects using js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

JSON string:

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

JSON object:

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

1. Convert JSON string to JSON Object

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

//由JSON字符串转换为JSON对象
var obj = eval('(' + str + ')');
Copy after login

or

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

or

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

Then, you can read it like this:

Alert(obj.name);
Alert(obj.sex);
Copy after login

Special note: If obj is originally a JSON object, Then after using the eval() function to convert (even multiple conversions) it will still be a JSON object, but there will be problems after using the parseJSON() function to process it (throwing a syntax exception).

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字符
Copy after login

or

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

Then, you can read it like this:

alert(last);
Copy after login

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 has modified the API, injecting both JSON.stringify() and JSON.parse() methods into the built-in object of Javascript, and the former becomes Object .toJSONString(), which 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 video tutorials: "JavaScript Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to convert json string to json object in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!