Home  >  Article  >  Web Front-end  >  Summary of mutual conversion methods between JSON strings and JSON objects

Summary of mutual conversion methods between JSON strings and JSON objects

php中世界最好的语言
php中世界最好的语言Original
2018-04-24 17:16:392761browse

This time I will bring you a summary of the mutual conversion methods of JSON strings and JSON objects. What are the precautions for the mutual conversion of JSON strings and JSON objects? The following is a practical case, let's take a look.

Method to convert json string to 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 str1 above must be converted into a JSON object first using the following method:

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 like this:

Alert(obj.name);
Alert(obj.sex);
Special note: If obj is originally a JSON object, then after using the eval() function to convert it (even if it is converted multiple times) it will still be a JSON object, but after using the parseJSON() function to process it, it will Something went wrong (throws 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 str=obj.toJSONString();
//将JSON对象转化为JSON字符
or

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

Summary:

We also saw type conversion above Generally speaking, there are two types, 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 hidden one here. Potential security issues 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 believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Adjustment of ajax execution order in jquery

Detailed explanation of Ajax and $.ajax instances

What are the steps to easily upload files with ajax html

The above is the detailed content of Summary of mutual conversion methods between JSON strings and JSON objects. 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