JS Read parsing JSON data

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data interchange format that is ideal for data interchange formats in a completely language-independent text format. At the same time, JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.

This article is mainly to the JS operation JSON Essentials to do a summary.     In JSON, there are two kinds of structures: objects and arrays. 1. An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pairs" are separated by "," (comma). The name is enclosed in quotation marks, and if the value is a string, it must be in parentheses, and the numeric type is not required. For example:
var o={"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": $, "topplaytime": "2009-08-20"};
2. An array is an ordered 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"}];
To facilitate the processing of JSON data, JSON provides a json.js package,: Http://www.json.org/json.js in the data transfer process, JSON is in the form of text, that is, a string, and JS operation is a JSON object, so, The conversion between a JSON object and a JSON string is key. For example:
json string: var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';     JSON object: var str2 = {"Name": "Cxh", "Sex": "Man"}; One, the JSON string to the JSON object to use the above str1, you must use the following to convert to a JSON object://From the JSON string to the JSON object var obj = eval (' (' + str + ') '); or var obj = Str.parsejson (); Converted from a JSON string to a JSON object or var obj = json.parse (str);     The JSON string is converted to a JSON object and then it can be read like this: Alert (obj.name);     Alert (Obj.sex);     Special attention: If obj is originally a JSON object, then the use of the Eval () function after conversion (even if it is multiple conversions) or JSON object, but the use of the Parsejson () function after processing will be questioned 
(throw syntax exception). 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 (); Converts a JSON object into a JSON character or Var last=json.stringify (obj); Converts a JSON object to a JSON character, alert (last); Data set var str= ' [{' name ': ' cxh ', ' sex ': ' Man '},{' name ': ' cxh1 ', ' sex ': ' man1 '}] '; var obj = Str.parsejson (); Alert (obj[0].name )
Note: In the above multiple essentials, in addition to the eval () 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 the Javascript built-in object, which becomes the object.tojsonstring (), and the latter becomes the Strin G.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () Essentials, your JSON package version is too low. Reference to the original address

JS Read parsing JSON data

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.