2. Summary of basic JSON knowledge (2)

Source: Internet
Author: User

JSON Definition

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write, and easy to parse and generate by machines. It is based on JavaScript in ecma262 language specification (-12 ).Programming Language. JSON uses a text format unrelated to the programming language, but also uses C-like languages (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on, these features make JSON an ideal data exchange format.

JSON is a subset of the object literal notation.

The JSON structure is based on the following two points:

• 1. in different languages of the set of "name/value" pairs, It is understood as an object, record, structure (struct), Dictionary (dictionary ), hash table and keyed list

• 2. The ordered list of values is interpreted as an array in most languages)

JSON syntax

Ajax (Asynchronous JavaScript and XML "(Asynchronous JavaScript and XML) is a web development technology used to create interactive web applications, it uses a client script to send a request to the server, while the server only retrieves this request instead of the previous request to retrieve the entire page. At the same time, the server compilation data volume is reduced, this makes the server response faster.

These requests Usually retrieve formatted XML for response, and then JavascriptCodeThe Analysis of XML responses to display the results complicate the JavaScript code. At this time, new things are emerging, JSON (JavaScript Object Notation), which enables JavaScript to easily read complex data structures.

Features:

1. Simple formatted data exchange

2. Easy reading and writing habits

3. Easy Machine Analysis and Operation

4. the eval () method can be used in JavaScript to conveniently read JSON data.

5. JSON-Supported languages: ActionScript, C, C #, ColdFusion, E, Java, JavaScript, ML, objective caml, Perl, PHP, Python, REBOL, Ruby, and Lua. [6]

Syntax analysis:

JSON can be used to encapsulate data. It is similar to the C language family, so it can be easily analyzed by the C language family.

1. The object is contained in a pair of {}. // similar definition struct, with types and variables

2. The object's attribute member uses a pair of [String: value]. The attributes and attributes are separated by commas, for example:

String: value, string: Value

3. array is included in a pair of [], for example:

[Elements]

4. Use commas to separate elements from elements.

Elements, Elements

5. Types of element values:

String, number, object, array, true, false, null

JSON

{"Menu ":{

"ID": "file ",

"Value": "file :",

"Popup ":{

"Menuitem ":[

{"Value": "new", "onclick": "createnewdoc ()"},

{"Value": "Open", "onclick": "opendoc ()"},

{"Value": "close", "onclick": "closedoc ()"}

]

}

}

}

XML

<Menu id = "file" value = "file">

<Popup>

<Menuitem value = "new" onclick = "createnewdoc ()"/>

<Menuitem value = "open" onclick = "opendoc ()"/>

<Menuitem value = "close" onclick = "closedoc ()"/>

</Popup>

</Menu>

An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. [7]

An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.

Value can be a string, number, true, false, null, object, or array enclosed by double quotation marks ). These structures can be nested.

A string is a collection of any number of Unicode characters enclosed by double quotes. It is escaped using a backslash. A character (character) is a separate string (character string ). A string is very similar to a C or Java string.

The value (number) is also very similar to the value in C or Java. Remove unused octal and hexadecimal formats. Except for some encoding details.

You can use the eval () method of the eval () function to call the Javascript compiler to convert JSON text into an object. Because JSON is an exact subset of JavaScript, the compiler can parse JSON text correctly and then generate an object structure.

Eval functions are highly efficient. However, it can compile and execute any JSProgramSo there will be security issues. Eval is used only when the source is trusted. This is usually the case in Web applications where the server provides basic pages and JSON data. In many cases, the source is untrusted. In particular, never trust clients.

If you are concerned about security, it is best to use the JSON Parser (A parsing function is available in JS scripts ). The JSON parser only recognizes JSON text, so it is safer.

The JSON string converter (stringifier) does the opposite, which converts the Javascript data structure to JSON text. JSON does not support the cyclic data structure. Therefore, you cannot submit the cyclic structure to the string converter. [8]

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.