Summary of javascript operations on JSON

Source: Internet
Author: User

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.
This article mainly summarizes the essentials for JS operations on JSON.
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:
Copy codeThe Code is as follows:
Var o = {"xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime "};

The name is a string. The value can be a string, a value, an object, a Boolean value, a serial table, or a null value.
The value has a serial table (Array): After one or more values are partitioned with ",", they are enclosed by "[", "]" to form such a list, shape:
[Collection, collection]
String: a string of characters enclosed.
Value: A combination of 0-9 numbers, which can be negative or decimal. You can also use "e" or "E" as an index.
Boolean value: true or false.
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:
Copy codeThe Code is as follows:
Var jsonranklist = [{"xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime": "" },{ "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.
In the data transmission process, json is transmitted in the form of text, that is, strings, while JS operates on JSON objects. Therefore, the conversion between JSON objects and JSON strings is the key. For example:
JSON string:
Copy codeThe Code is as follows:
Var str1 = '{"name": "cxh", "sex": "man "}';

JSON object:
Copy codeThe Code is as follows:
Var str2 = {"name": "cxh", "sex": "man "};

1. convert a JSON string to a JSON object
To use str1 above, you must use the following to convert to a JSON object first:
// Converts a JSON string to a JSON object
Copy codeThe Code is as follows:
Var obj = eval ('+ str + ')');

Or
Copy codeThe Code is as follows:
Var obj = str. parseJSON (); // converts a JSON string to a JSON object.

Or
Copy codeThe Code is as follows:
Var obj = JSON. parse (str); // converts a JSON string to a JSON object.
[Html]
Then, you can read:
[Code]
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 () after the function is processed, there will be questions (a syntax exception is thrown ).
2. You can use toJSONString () or the global essentials JSON. stringify () to convert a JSON object to a JSON string.
For example:
Copy codeThe Code is as follows:
Var last = obj. toJSONString (); // converts a JSON object to a JSON character

Or
Copy codeThe Code is as follows:
Var last = JSON. stringify (obj); // converts a JSON object to a JSON character
Alert (last );

Note:
Among the above multiple essentials, except that the eval () function is provided by js, all other essentials come from the json. js package. The new JSON version modifies the API and changes JSON. stringify () and JSON. the two essentials of parse () are injected into the built-in Javascript Object. The former becomes the Object. toJSONString (), and the latter is a String. parseJSON (). If you cannot find the toJSONString () and parseJSON () essentials, it indicates that your json package version is too low.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.