JSON Quick Start Guide __json

Source: Internet
Author: User
Tags serialization
What is JSON?

JSON(JavaScript Object notation), a syntax for storing and exchanging text information, a lightweight text data interchange format, similar to XML, but smaller, faster, and easier to parse than XML. notation
JSON is independent of a variety of programming language platforms, and almost all major programming languages have built-in support for JSON data formats. JSON syntax rules

The JSON syntax is a subset of the JavaScript Object notation syntax: Data in the form of a name/value pair representing data in comma-delimited curly braces save an array of JSON values

A JSON name value pair includes a field name (enclosed in double quotes), a colon followed by a value, such as:

"username": "Jessica"

JSON values can contain several types: numeric (integer and floating-point) strings (in double quotes) logical value (TRUE or FALSE) an array (in brackets) an object (in curly braces) a null JSON object and a JSON array

The JSON object is contained in curly braces and can contain multiple name/value pairs whose values can be arrays; The JSON array is contained in square brackets and can contain multiple objects. JSON can either begin with curly braces or begin with square brackets , and JSON objects and JSON arrays can be nested with each other, such as:

{"
    users": [
        {"username": "Jacky", age:30},
        {"username": "Jessica", age:23},
        {"username": "James", AGE:42}
    ]
}
JSON fileThe suffix of the JSON file is: ". JSON" the MIME type of the JSON file is: "Application/json" JSON parsing and serialization

The early JSON parser is basically the eval () function that uses JavaScript. Because JSON is a subset of the JavaScript syntax, the eval () function can parse and return JavaScript objects and arrays.
The ECMAScript 5 defines the behavior of parsing JSON, defining global object JSON. JSON parsing

Use json.parse () to parse the string into a JSON object or array.

Returns the JSON object
var jsonStr1 = ' {username ': ' Jessica ', ' Age ':} ';
var jsonobj = Json.parse (JSONSTR1);
Console.log (Jsonobj.username); Jessica
Console.log (jsonobj.age)//23

//return JSON array
var jsonStr2 = ' [{' username ': ' Jacky ', ' Age ': 30}, {' Username ":" Jessica "," Age ":}] ';
var Jsonarr = Json.parse (JSONSTR2);
for (Var i=0;i<jsonarr.length;i++) {
    console.log (jsonarr[i].username);//jacky Jessica
}
JSON serialization

Serializes a JSON object or array into a string using json.stringify () .

Serialized JSON object
var jsonobj = {
    "username": "Jessica",
    "Age":
};
var jsonStr1 = json.stringify (jsonobj);
Console.log (JSONSTR1); {' username ': ' Jessica ', ' age ': '

/' serialized JSON array
var jsonarr = [
    {' username ': ' Jacky ', ' Age ':},
    {' Username ":" Jessica "," Age ":}
];
var jsonStr2 = json.stringify (Jsonarr, NULL, 2);
Console.log (JSONSTR2); 
/* Output Result:
[
  {
    "username": "Jacky",
    "Age":
  },
  {
    "username": "Jessica",
    "" Age ":
  }
]
* *

* In addition, when the serialized string is longer, you can indent the string by setting the third parameter of the Json.stringify () method, which is convenient for debugging.

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.