JS operation JSON summary JS operation JSON Summary

Source: Internet
Author: User
JS operation JSON Summary

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:

VaR o = {"xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime "};

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": "" },{ "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:

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 to convert to a JSON object first:

// Converts a JSON string to a 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 () 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:

VaR last = obj. tojsonstring (); // converts a JSON object to a JSON character

Or

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.

 

Original article: http://www.cnblogs.com/worfdream/articles/1956449.html

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:

VaR o = {"xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime "};

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": "" },{ "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:

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 to convert to a JSON object first:

// Converts a JSON string to a 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 () 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:

VaR last = obj. tojsonstring (); // converts a JSON object to a JSON character

Or

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.

 

Original article: http://www.cnblogs.com/worfdream/articles/1956449.html

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.