JSON (JavaScript Object notation) is a lightweight data interchange format that uses a completely language-independent text format and is an ideal data interchange format. Also, JSON is the native format of JavaScript, which means that processing JSON data in JavaScript does not require any special APIs or toolkits.
This article is mainly on the JS operation of the JSON essentials to do the summary.
In JSON, there are two types of structures: objects and arrays.
1. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pairs", separated by "," (comma). The name is enclosed in quotation marks; If the value is a string, it must be in parentheses, and the numeric type is not required. For example:
Copy Code code as follows:
var o={"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": "," Topplaytime ":" 2009-08-20 "};
2. An array is an ordered set of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma).
For example:
Copy Code code as follows:
var jsonranklist=[{"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": ", Topplaytime": "2009-08-20"},{"Xlid": "Zd", " Xldigitid ": 123456," Topscore ": 1500," Topplaytime ":" 2009-11-20 "}];
To handle JSON data conveniently, JSON provides a json.js package, download address: http://www.json.org/json.js
In the data transfer process, JSON is passed in the form of text, a string, and JS is the JSON object, so the conversion between JSON and JSON strings is critical. For example:
JSON string:
Copy Code code as follows:
var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';
JSON object:
Copy Code code as follows:
var str2 = {"Name": "Cxh", "Sex": "Man"};
one, JSON string converted to JSON object
To use the above str1, you must use the following to convert to the JSON object:
Copy Code code as follows:
Convert from 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 to JSON object
Then, you can read this:
Copy Code code as follows:
Alert (Obj.name);
Alert (Obj.sex);
Special attention: If obj is a JSON object, then using the eval () function to convert (even multiple conversions) is a JSON object, but using the Parsejson () function can be problematic (throwing syntax exceptions).
second, can use toJSONString ()or Global Essentials json.stringify () converts a JSON object into a JSON string.
For example:
Copy Code code as follows:
var last=obj.tojsonstring (); Convert a JSON object to a JSON character
Or
var last=json.stringify (obj); Convert a JSON object to a JSON character
alert (last);
Pay attention to:
Of the above essentials, in addition to the eval () function is JS from the band, the other points are from the Json.js package. The new version of JSON modifies the API and injects the json.stringify () and Json.parse () Two essentials into Javascript's built-in object, which becomes object.tojsonstring (), and the latter becomes Strin G.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () Essentials, your JSON package version is too low.