JS operation JSON method summary (recommended), jsjson
JSON Overview:
JSON (JavaScript Object Notation) is a lightweight data exchange format. It adopts a completely language-independent text format and is an ideal data exchange format. JSON is a native JavaScript format, which means that no special API or toolkit is required to process JSON data in JavaScript.
JSON: JavaScript Object Notation (JavaScript Object Notation ).
JSON is the syntax for storing and exchanging text information. Similar to XML.
JSON is smaller, faster, and easier to parse than XML.
JSON syntax rules
JSON syntax is a subset of the syntax of JavaScript Object Notation.
Data in name/value pairs
Data is separated by commas (,).
Brackets save objects
Square brackets Save the Array
Well, the above is not the focus of this article. This article mainly focuses onThe JS method for JSON operations is as follows:End.
In JSON, there are two structures: objects and arrays.
1. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. The name is enclosed in quotation marks. If the value is a string, it must be enclosed in parentheses, but not numeric. For example:
var o={"xlid":"cxh","xldigitid":123456,"topscore":2000,"topplaytime":"2009-08-20"};
2. An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.
For example:
var jsonranklist=[{"xlid":"cxh","xldigitid":123456,"topscore":2000,"topplaytime":"2009-08-20"},{"xlid":"zd","xldigitid":123456,"topscore":1500,"topplaytime":"2009-11-20"}];
JSON provides a JSON. js package: http://www.json.org/json.js for easy processing of json data.
During data transmission, json is transmitted in the form of text, that is, strings, while JS operates on JSON objects. Therefore, mutual conversion between JSON objects and JSON strings is the key. For example:
JSON string:
var str1 = '{ "name": "cxh", "sex": "man" }';
JSON object:
var str2 = { "name": "cxh", "sex": "man" };
1. convert a JSON string to a JSON object
To use str1 above, you must use the following method to first convert it to a JSON object:
// Convert the JSON string to the JSON object var obj = eval ('+ str + ')');
Or
Var obj = str. parseJSON (); // converts a JSON string to a JSON object.
Or
Var obj = JSON. parse (str); // converts a JSON string to a JSON object.
Then, you can read:
Alert(obj.name);Alert(obj.sex);
NOTE: If obj is a JSON object, it is still a JSON object after eval () function conversion (even Multiple conversions), but parseJSON () is used () A problem occurs after the function is processed (a syntax exception is thrown ).
2. You can use toJSONString () or the global method JSON. stringify () to convert a JSON object to a JSON string.
For example:
Var last = obj. toJSONString (); // converts a JSON object to a JSON character
Or
Var last = JSON. stringify (obj); // converts a JSON object to the JSON character alert (last );
Friendly reminder of the customer's house:
Among the methods above, except that the eval () function is provided by js, several other methods are from the json. js package. The new JSON version modifies the API and changes JSON. stringify () and JSON. both parse () methods are injected into the built-in Javascript Object, and the former becomes the Object. toJSONString (), and the latter is a String. parseJSON (). If you cannot find the toJSONString () and parseJSON () methods, the version of your json package is too low.
The above is a summary (recommended) of the JS operation JSON method described in the editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!