Home  >  Article  >  Web Front-end  >  Detailed explanation of mutual conversion examples between JSON strings and JSON objects

Detailed explanation of mutual conversion examples between JSON strings and JSON objects

高洛峰
高洛峰Original
2017-01-18 10:03:421411browse

The examples in this article describe the methods of converting JSON strings and JSON objects into each other. Share it with everyone for your reference, the details are as follows:

Method to convert json string into json object. During the data transmission process, json is transmitted in the form of text, that is, a string, and JS operates on JSON objects, so the conversion between JSON objects and JSON strings is key

For example:

JSON string:

var str = '{ "name": "name1","sex": "m" }';

JSON object:

var obj = { "name": "name1", "sex": "w" };

1. Convert JSON string to JSON object

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

var obj = eval('(' + str + ')');
//由JSON字符串转换为JSON对象,必须把文本包围在括号中,这样才能避免语法错误: "(" + str+ ")

or

var obj = $.parseJSON(str);
// 将JSON字符串转化为JSON对象 (jQuery)

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, then 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 to process it.

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

For example:

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

or

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

Summary:

We also saw the progress above Generally speaking, there are two types of type conversion. One is the parser that comes with JavaScript, and the other is the JSON parser. The JavaScript parser can compile and execute any JavaScript code, so there is a potential security hidden here. The problem is that the JSON parser can only recognize JSON text and does not compile scripts, so it is safer and the JSON parser is faster.

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.

I hope this article will be helpful to everyone in JavaScript programming.

For more detailed examples of mutual conversion between JSON strings and JSON objects, please pay attention to the PHP Chinese website for related articles!

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