The Ajax-json of JavaScript

Source: Internet
Author: User

Introduction: JSON is a better way to read structured data in JavaScript. Because JSON data can be passed directly to Eval (), and you do not have to create DOM objects. JSON is a data format, not a programming language, and although it has the same syntactic form, JSON does not belong to JavaScript. And it's not just JavaScript that uses JSON, after all, JSON is just a data format. Many programming languages have parsers and serializers for JSON.

Second, the grammar

The JSON syntax can represent three types of values

1. Simple value: Using the same syntax as JavaScript, you can represent strings, numeric values, Booleans, and Null in JSON. Note: JSON does not support special values in Javasciprt: undefined.

2. Object: As a complex data type, the object represents a set of unordered key-value pairs. The value in each key-value pair can be a simple value, or it can be a value of a complex data type.

3. Array: An array is also a complex data type that represents a list of ordered values that can be accessed by a numeric index. The value of an array can also be a simple value, an object, an array.

2.1 Simple values

The representation of simple values in JSON can be string, numeric, Boolean, and null!

Note: The above data are the same as in JavaScript, only the string, in the JS string can be expressed in single quotation marks, but in the JSON string using single quotation marks will be error!

2.2 Objects

The object in JS, the code is as follows:

var person={  name:"Zhang San", age  :+}; var person={  "name": "Zhang San",  "age": +};

Both of the above methods can represent objects in JS, but there is no concept of variables in JSON. Second, there is no semicolon at the end (because it is not a javascipt statement, so it does not need a semicolon),

So in a JSON object, the object's property must be a string (that is, double quotes), the value of the property can be a simple value, an array, an object, so the object in JSON behaves as follows:

{"name": "Zhang San","age": +}

Note: The property name of the object must be double-quoted!

Iii. parsing and serialization of JSON

JSON is popular, having a syntax similar to JavaScript is not the whole reason, and more importantly, the JSON data structure can be parsed into useful javascipt objects. Compared to parsing XML data into DOM documents and extracting data from it, the benefits of JSON parsing into JavaScript objects are obvious! The code is as follows:

[{"title": "Book_one",  "year": "},{", "title": "Book_two",  "year": " "},{" title ":" Book_three "  ," year ":", "},

The next step is to get the name of the third book, which is converted to a JavaScript object by JSON.

Books[2].title

Parsing into DOM elements through XML, in getting:

document.getElementsByTagName ("book") [2].getattribute ("title");

Compare the two methods and know why JSON is welcome by JAVASCIPRT Developers!

The Ajax-json of JavaScript

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.