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:
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:
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:
var str1 = '{ "name": "cxh", "sex": "man" }';
JSON object:
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 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:
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:
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.