Home  >  Article  >  Web Front-end  >  Detailed introduction to JS operation JSON

Detailed introduction to JS operation JSON

php中世界最好的语言
php中世界最好的语言Original
2018-04-23 10:25:401037browse

This time I will bring you a detailed introduction to JS operation JSON. What are the precautions for JS operation JSON? The following is a practical case, let's take a look.

JSON is a lightweight data exchange format that uses a completely language-independent text format and is an ideal data exchange format. This article introduces to you a summary of JS operation methods for JSON. It is very good and has reference value. Friends who are interested can learn together

JSON Overview:

JSON(JavaScript Object Notation) is a lightweight data exchange format that uses a completely language-independent text format and is an ideal data exchange format. At the same time, JSON is a native JavaScript format, which means that processing JSON data in JavaScript does not require any special API or toolkit.

JSON: JavaScript ObjectNotation (JavaScript Object Notation).

JSON is a syntax for storing and exchanging text information. Similar to XML.

JSON is smaller, faster, and easier to parse than XML.

JSON syntax rules

JSON syntax is a subset of JavaScript object notation syntax.

Data is in name/value pairs

Data is separated by commas

Curly braces save object

Square brackets save array

OK Okay, the above is not the focus of this article. This article mainly summarizes the methods of JS to operate JSON.

In JSON, there are two structures: objects and arrays.

1. An object starts with "{" (left bracket) and ends with "}" (right bracket). Each "name" is followed by a ":" (colon); "name/value" pairs are separated by a "," (comma). The name is enclosed in quotation marks; if the value is a string, it must be enclosed in parentheses, and if it is a numeric value, it is not required. For example:

var o={"xlid":"cxh","xldigitid":123456,"topscore":2000,"topplaytime":"2009-08-20"};

2. An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Values ​​are separated by "," (comma).

For example:

var jsonranklist=[{"xlid":"cxh","xldigitid":123456,"topscore":2000,"topplaytime":"2009-08-20"},{"xlid":"zd","xldigitid":123456,"topscore":1500,"topplaytime":"2009-11-20"}];

In order to process JSON data conveniently, JSON provides the json.js package, download address: http://www.json.org/json.js

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 the key. For example:

JSON string:

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

JSON object:

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

1. JSON Convert string to JSON object

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

//由JSON字符串转换为JSON对象
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, then use the eval() function to convert it (even if Is it multiple conversions) or 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字符

or

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

Script House would like to remind everyone that you need to pay attention to the following issues:

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:

!=,==,!==,===Usage summary

JS debugging debug method summary

The above is the detailed content of Detailed introduction to JS operation JSON. 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