JS parsing JSON method

Source: Internet
Author: User

Inin JSON, there are two kinds of structures: objects and arrays.

JSON (JavaScript Object Notation) is a lightweight data interchange format, with a completely language-independent text format, ideal for data interchange formats

     1.   An object begins with the (opening parenthesis), Span lang= "en-US" > "}" (closing parenthesis) ends. Each " name " is followed by a (colon); Span lang= "en-us" > " name / value   to " applied between  , (comma) delimited. The   name is enclosed in quotation marks, and if the value is a string, it must be in parentheses, and the numeric type does not need . For example:

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

2. An array isanordered collection of values (value). An array begins with "["(the left square bracket), and"]"(the right square bracket) ends. Use ","(comma) to separate values.

For example:

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

conversion between JSON object and JSON string

In the data transfer process,JSON is passed in the form of text, which is a string, and JS operates on a JSON object, so the conversion between the JSON object and the JSON string is key. For example:

JSON string :

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

JSON object :

var str2 = {"Name": "Cxh", "Sex": "Man"};

One,JSON string converted to JSON object

To use the above str1, you must use the following to turn the lead into a JSON object:

convert from JSON string to JSON object

var obj = Eval_r (' (' + str 1+ ') ');

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:

Alert (Obj.name);

Alert (Obj.sex);

Json.parse () method
This method only supports ie8/firefox3.5+/chrome4/safari4/opera10 and above, these browsers are already close to the standard, the Tojson method is implemented by default.

function ToJson (str) {
return Json.parse (str);
}

Special attention: if obj JSON   eval () function after conversion (even if multiple conversions) or json   parsejson () function will have a question (throw a syntax exception) after processing

Second, you can use tojsonstring () or Global Essentials json.stringify () to convert the JSON object into a JSON string.

For example:

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:

In the above multiple essentials, in addition to the eval_r () function is js comes from the other, many other essentials are from the json.js package. the new version of JSON modifies the API to inject json.stringify () and json.parse () Two essentials into Javascript's built- in objects, which become C16>object.tojsonstring (), while the latter becomes String.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () Essentials, your JSON package version is too low

JS parsing JSON method

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.