JS Operation JSON Essentials detailed summary _javascript skill

Source: Internet
Author: User
Tags string to json
JSON (JavaScript Object notation) is a lightweight data interchange format that uses a completely language-independent text format and is an ideal data interchange format. Also, JSON is the native format of JavaScript, which means that processing JSON data in JavaScript does not require any special APIs or toolkits.
This article is mainly on the JS operation of the JSON essentials to do the summary.
In JSON, there are two types of structures: objects and arrays.
1. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pairs", separated by "," (comma). The name is enclosed in quotation marks; If the value is a string, it must be in parentheses, and the numeric type is not required. For example:
Copy Code code as follows:

var o={"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": "," Topplaytime ":" 2009-08-20 "};

2. An array is an ordered set of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma).
For example:
Copy Code code as follows:

var jsonranklist=[{"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": ", Topplaytime": "2009-08-20"},{"Xlid": "Zd", " Xldigitid ": 123456," Topscore ": 1500," Topplaytime ":" 2009-11-20 "}];

To handle JSON data conveniently, JSON provides a json.js package, download address: http://www.json.org/json.js
In the data transfer process, JSON is passed in the form of text, a string, and JS is the JSON object, so the conversion between JSON and JSON strings is critical. For example:
JSON string:
Copy Code code as follows:

var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';

JSON object:
Copy Code code as follows:

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 the JSON object:
Copy Code code as follows:

Convert from JSON string to JSON object
var obj = eval (' (' + str + ') ');
Or
var obj = Str.parsejson (); Convert from JSON string to JSON object
Or
var obj = json.parse (str); Convert from JSON string to JSON object

Then, you can read this:
Copy Code code as follows:

Alert (Obj.name);
Alert (Obj.sex);

Special attention: If obj is a JSON object, then using the eval () function to convert (even multiple conversions) is a JSON object, but using the Parsejson () function can be problematic (throwing syntax exceptions).

second, can use toJSONString ()or Global Essentials json.stringify () converts a JSON object into a JSON string.
For example:
Copy Code code as follows:

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 to:
Of the above essentials, in addition to the eval () function is JS from the band, the other points are from the Json.js package. The new version of JSON modifies the API and injects the json.stringify () and Json.parse () Two essentials into Javascript's built-in object, which becomes object.tojsonstring (), and the latter becomes Strin G.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () Essentials, your JSON package version 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.