Home > Web Front-end > JS Tutorial > Summary of essentials for operating JSON in javascript_json

Summary of essentials for operating JSON in javascript_json

WBOY
Release: 2016-05-16 17:47:17
Original
1135 people have browsed it

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 JavaScript native format, which means that processing JSON data in JavaScript does not require any special API or toolkit.
This article mainly summarizes the essentials of JS operating 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 "," (comma). The name is enclosed in quotes; the value must be enclosed in parentheses if it is a string, but not if it is a numeric value. For example:

Copy code The code is as follows:

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

The name is a string; the value can be a string, a A numeric value, an object, a Boolean value, a serialized list, or a null value.
There is a serial table (Array) of values: one or more values ​​are partitioned with "," and enclosed with "[", "]" to form such a list, in the form:
 [collection , collection]
String: A string of characters enclosed by "".
Value: a series of number combinations from 0 to 9, which can be negative numbers or decimals. It can also be expressed in exponential form using "e" or "E".
Boolean value: expressed as true or false.
2. An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Use "," (comma) to separate values.
For example:
Copy code The code is as follows:

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
In the data transmission process, json is passed in the form of text, that is, a string, and JS operates on JSON objects. Therefore, the conversion between JSON objects and JSON strings is key. For example:
JSON string:
Copy code The code is as follows:

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

JSON object:
Copy code The code is as follows:

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

1. Convert JSON string to JSON object
To use str1 above, you must use the following to convert it into JSON object first:
//Convert from JSON string to JSON object
Copy code The code is as follows:

var obj = eval('(' str ')');

or
Copy code The code is as follows:

var obj = str.parseJSON(); //Convert JSON string to JSON object

or
Copy code The code is as follows:

var obj = JSON.parse(str); //Convert JSON string to JSON object
[html]
Then, you can read it like this Take:
[code]
Alert(obj.name);
Alert(obj.sex);

Special attention: if obj is originally a JSON object, then use After conversion by the eval() function (even if it is converted multiple times), it is still a JSON object, but there will be questions after processing with the parseJSON() function (a syntax exception will be thrown).
2. You can use toJSONString() or the global method JSON.stringify() to convert the JSON object into a JSON string
For example:
Copy code The code is as follows:

var last=obj.toJSONString(); //Convert JSON object to JSON character

or
Copy code The code is as follows:

var last=JSON.stringify(obj ; In addition, many other methods 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.
Related labels:
source:php.cn
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