JSON (JavaScript Object Notation) is a lightweight data interchange format that is ideal for data interchange formats in a completely language-independent text format. At the same time, JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.
In JSON, there are three structures: objects, arrays, and nested objects in a group.
1. An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pairs" are separated by "," (comma). The name is enclosed in quotation marks, and if the value is a string, it must be in parentheses, and the numeric type is not required.
For example:
var o={"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": $, "topplaytime": "2009-08-20"};
2. An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.
For example:
var jsonranklist=[{"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": $, "topplaytime": "2009-08-20"},{"Xlid": "Zd", " Xldigitid ": 123456," Topscore ":" Topplaytime ":" 2009-11-20 "}];
To facilitate the processing of JSON data, JSON provides the json.js package:http://www.json.org/json.js
3. Complex format: combination of array format and object format.
For example:
var str3 = [{"Name": "Cxh", "Sex": "Man"},{"name": "Zzl", "Sex": "Woman"},{"name": "Bobo", "Sex": "Woman"}];
Multiple objects are nested within the array.
In the data transfer process, JSON is passed in the form of text, which is a string, and JS operates on a JSON object, so the conversion between the JSON object and the JSON string is key.
For example:
JSON string:
var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';
JSON object:
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 a JSON object:
//From JSON string to JSON object
var obj = eval (' (' + str + ') ');
or
var obj = Str.parsejson ();
or
var obj = json.parse (str);
Then, you can read this:
Alert (obj.name);
Alert (Obj.sex);
Gets the key value in the JSON string by traversing:
for (var key in str2) {
Console.log (key)
}
special attention:
If obj is inherently a JSON object, then the use of the Eval () function after conversion (even if multiple conversions) is a JSON object, but the use of the Parsejson () function is problematic (throwing a syntax exception).
Second, convert the JSON object to a JSON string.
You can use toJSONString () or Global Essentials json.stringify () to convert a JSON object into a JSON string.
For example:
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:
In the above multiple essentials, in addition to the eval () function is JS comes from, the other main points are from the Json.js package. The new version of JSON modifies the API to inject json.stringify () and Json.parse () Two essentials into the Javascript built-in object, which becomes the object.tojsonstring (), and the latter becomes the Strin G.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () Essentials, your JSON package version is too low.
JS Operation JSON Summary