JS operation 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 introduces js json operations: 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"}]; To process JSON data conveniently, JSON provides JSON. JS package,: callback. Example: JSON string: var str1 = '{"name": "cxh", "sex": "Man"}'; JSON object: var str2 = {"name ": "cxh", "sex": "Man"}; 1. to convert a JSON string to a JSON object, use str1 above. You must first convert it to a JSON object using the following method: // 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 and then reads: Alert (obj. name); alert (obj. sex); Special Note: If obj is a JSON object, after using the eval () function conversion (even Multiple conversions) or JSO N object, but the parsejson () function is used to handle the problem (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). Note: In the preceding methods, except that the eval () function is provided by JS, several other methods are from 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.

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 document describes how to operate JSON in javascript:

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.

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. to convert a JSON string to a JSON object, use str1 above. You must first convert it to a JSON object using the following method:

// 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 a JSON character
Alert (last );

Note: 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.

 

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.