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

How to convert javascript object to json

藏色散人
Release: 2021-10-26 15:19:34
Original
17435 people have browsed it

How to convert javascript object to json: 1. Convert the JSON object into JSON characters through the "var last=obj.toJSONString()" method; 2. Convert the JSON object to JSON characters through "var last=JSON.stringify(obj);" way to convert.

How to convert javascript object to json

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to convert javascript object to json?

Conversion of object and json in JavaScript

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 str1 above, you must use the following method to convert it to JSON object first:

var obj = eval('(' + str + ')');//由JSON字符串转换为JSON对象
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 use eval() After function conversion (even multiple conversions), it is still a JSON object, 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字符
Copy after login

or

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

Note:

Among the above methods, except the eval() function, which comes with js , several other methods are 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 basic tutorial]

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

Related labels:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template