JS Operation JSON Summary

Source: Internet
Author: User
Tags string to json

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 mainly is the JS operation JSON method to make the 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 ' pair ' is 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 the Json.js package: http://www.json.org/json.js
In the process of data transfer, JSON is passed in the form of text, which is a string, and JS is the JSON object, so the conversion between JSON object and JSON string is the 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 first convert to a JSON object using the following method:
Converted from JSON string to JSON object source: Exam Big
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:
Alert (Obj.name);
Alert (Obj.sex);
Special Note: If obj is originally a JSON object, then using the eval () function after conversion (even if multiple conversions) is a JSON object, but there is a problem with the Parsejson () function (throwing a syntax exception).
Second, the JSON object can be converted to a JSON string using tojsonstring () or global Method Json.stringify ().
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);
Attention:
In the above several methods, in addition to the eval () function is JS own, the other several methods are from the Json.js package. The new version of JSON modifies the API to inject json.stringify () and Json.parse () two methods into the JavaScript built-in object, which becomes the object.tojsonstring (), and the latter becomes String.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () methods, your JSON package version is too low.

Json.parse () and json.stringify ()

var str = ' {' name ': ' Huangxiaojian ', ' age ': ' 23 '} '

Results:

Json.parse (str)

Object
  1. Age: "All"
  2. Name: "Huangxiaojian"
  3. __proto__: Object


Note: Single quotes are written in {}, each property name must be in double quotation marks, or an exception will be thrown.


Stringify () is used to parse out a string from an object, such as

var a = {A:1,b:2}

Results:

Json.stringify (a)

"{" A ": 1," B ": 2}"

JS Operation JSON Summary

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.