Home > Web Front-end > JS Tutorial > body text

Detailed summary of JS operation JSON essentials_javascript skills

WBOY
Release: 2016-05-16 17:24:21
Original
1060 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"};

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:
Copy the code The code is as follows:

//Convert JSON string to JSON object
var obj = eval('(' str ')');
or
var obj = str.parseJSON(); //Convert from JSON string to JSON object
or
var obj = JSON.parse(str); //Convert from JSON string For the JSON object

, you can then read it like this:
Copy the code The code is as follows:

Alert(obj.name);
Alert(obj.sex);

Special attention: 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 questions (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:
Copy code The code is as follows:

var last=obj.toJSONString( ); //Convert JSON objects into JSON characters
or
var last=JSON.stringify(obj); //Convert JSON objects into JSON characters
alert(last);

Note:
Among the above methods, except that the eval() function comes with js, 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