JSON syntax, parsing JSON, serializing JSON

Source: Internet
Author: User

Json (JavaScript Object Notation) JavaScript objects notation

JSON is a strict subset of JavaScript, using some of the schema classes in JavaScript to represent structured data, because JSON is passed directly to Eavl (), and is no more than creating DOM objects

Syntax for 1,json:

    • Number (integer or floating point)
    • String (in double quotes)
    • Logical value (TRUE or FALSE)
    • Array (in square brackets)
    • Object (in curly braces)
    • Null

JSON can represent three types of values: 1, simple values, you can use the same syntax as JavaScript, you can represent strings, numeric values, Booleans, and Null in JSON, but undefined is not supported

2, object, a set of unordered key-value pairs, values can be simple values, or complex data type values.

3, array, ordered list of values, values can be simple values, objects or arrays

JSON does not support variables, functions, or object instances,

1.1, Simple value: 5 "Hello World" string must be in double quotation marks

1.2 Object: The literal form of a JSON object, you must enclose the attribute in double quotation marks.  For example: {"name": "ABCD", "age": 10; } and JS literal compared to the first, there is no declaration of variables (JSON does not have the concept of variables); Next there is no semicolon at the end;

For example: {"name": "ABLC", "Age": "School": {"name": "Li", "Location": "Hangzhou"}} The "School" object contains a JSON object

1.3 Array: The JSON array takes the literal form of an array in JavaScript

2 Array of JSON objects

    1. {"Employees": [{"FirstName": "Bill", "LastName": "Gates"},{"FirstName": "George", "LastName": "Bush"},{"FirstName": " Thomas "," LastName ":" Carter "}]}          //This is an employee object that contains three employee objects

!!!!!!!!!!! Same reason:

For example, you can create a new JavaScript variable, and then assign the JSON-formatted data string directly to it:

var people = {"Programmers": [{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},

{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},

{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}

],

"Authors": [

{"FirstName": "Isaac", "LastName": "Asimov", "Genre": "Science Fiction"},

{"FirstName": "Tad", "LastName": "Williams", "Genre": "Fantasy"},

{"FirstName": "Frank", "LastName": "Peretti", "Genre": "Christian Fiction"}

],

"Musicians": [

{"FirstName": "Eric", "LastName": "Clapton", "instrument": "Guitar"},

{"FirstName": "Sergei", "LastName": "Rachmaninoff", "Instrument": "Piano"}

] }

This is very simple; now people contains the JSON-formatted data you saw earlier. However, this is not enough, because the way data is accessed doesn't seem to be obvious.

Here people is the JS variable object!!!!! , his value is the value of JSON format, "programmers", "authors", "Musicians": Is the key value, for the value is three JSON array!!!!! The array includes the Person object,

accessing data

Although it doesn't seem obvious, the long string above is really just an array, and after you put the array into a JavaScript variable, it's easy to access. In fact, you simply represent the array element with a dot notation. So, to access the last name of the first entry in the programmers list, just use the following code in JavaScript:

People.programmers[0].lastname;




2,var employees = [ {"FirstName": "Bill", "LastName": "Gates"}, {"FirstName": "George", "LastName": "Bush"}, {"FirstName": "Thomas", "LastName": "Carter"} ];
It is a JSON array that contains three objects. can use Employee[0].lastname; To access, as with the JS array.
This access JSON array is the same as the JS array. Employees[0].firstname

3, parsing and serialization

Parses a JSON data structure into a JavaScript object for easy access

3.1 JSON Object

ECMAScript defines the global object JSON, so it can be parsed directly with the Eval () function, and using the eval () function to parse the JSON data structure can be risky because malicious code can be executed

There are two methods of JSON objects: Stringify () and parse ();

In the simplest case, two methods are used to serialize a JavaScript object into a JSON string and parse the JSON string into native JavaScript values.

            JSON.stringify()The Replacer method converts a JavaScript value to a JSON string that can be substituted if a function is specified, or if a replacer array is specified, optionally including only the specified property.

Grammar  JSON.stringify(value[, replacer [, space]])

Http://www.jb51.net/article/29893.htm

This string does not include any whitespace characters or indents.

Json.parse () can get the corresponding JavaScript value.

JSON syntax, parsing JSON, serializing JSON

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.