JSON (JavaScript Object Notation)-lightweight data exchange format

Source: Internet
Author: User

 

JSON(JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript programming language, standard ECMA-262 3rd edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:

    • A collection of name/value pairs ). In different languages, it is understoodObject), Record, structure (struct), Dictionary, hash table, keyed list, or associative array ).
    • An ordered list of values ). In most languages, it is understood as an array ).

These are common data structures. In fact, most modern computer languages support them in some form. This makes a data format based on these structuresProgramming LanguageExchange between them becomes possible.

JSON has the following forms:

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.

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 (Value) Can be a string enclosed by double quotation marks (String), Value (number ),True,False,NullObject or array ). These structures can be nested.

String (StringIs a set of any number of Unicode characters enclosed by double quotation marks, which are escaped using a backslash. A character (character) is a separate string (character string ).

String (String) Is very similar to a C or Java string.

Value (Number) Is also very similar to the values in C or Java. Remove unused octal and hexadecimal formats. Except for some encoding details.

JSON usage:

JSON represents a JavaScript Object in a specific string. If a string with such a form is assigned to any JavaScript variable, the variable becomes an object reference, and the object is constructed by the string. It seems like a bit of an interface, we still use instances to describe.
Assume that we need to create a user object with the following attributes
User ID
User Name
User email
You can use the following JSON format to represent a user object:
{"Userid": 11, "name": "truly", "email": "zhuleipro ◎ hotmail.com"}. If you assign this string to a javascript variable, then you can directly use any property of the object.
Complete Code :
<SCRIPT>
VaR user = {"userid": 11, "name": "truly", "email": "zhuleipro ◎ hotmail.com "};
Alert (user. Name );
</SCRIPT> in actual use, it may be more complex. For example, we define a more detailed structure for name so that it has firstname and lastname:
{"Userid": 11, "name": {"firstname": "truly", "lastname": "zhu"}, "email ": "zhuleipro ◎ hotmail.com"} complete code:
<SCRIPT>
VaR user = {"userid": 11, "name": {"firstname": "truly", "lastname": "zhu"}, "email ": "zhuleipro ◎ hotmail.com "};
Alert (user. Name. firstname );
</SCRIPT> now we have a new requirement. A page requires a user list, not just a single user information, so we need to create an array of user lists.
The following code defines the user list in JSON format:
[
{"Userid": 11, "name": {"firstname": "truly", "lastname": "zhu"}, "email": "zhuleipro ◎ hotmail.com "},
{"Userid": 12, "name": {"firstname": "Jeffrey", "lastname": "Richter"}, "email": "XXX ◎ xxx.com "},
{"Userid": 13, "name": {"firstname": "Scott", "lastname": "Gu"}, "email": "xxx2 ◎ xxx2.com "}
]
Complete code:
<SCRIPT>
VaR userlist = [
{"Userid": 11, "name": {"firstname": "truly", "lastname": "zhu"}, "email": "zhuleipro ◎ hotmail.com "},
{"Userid": 12, "name": {"firstname": "Jeffrey", "lastname": "Richter"}, "email": "XXX ◎ xxx.com "},
{"Userid": 13, "name": {"firstname": "Scott", "lastname": "Gu"}, "email": "xxx2 ◎ xxx2.com "}
];
Alert (userlist [0]. Name. firstname );
</SCRIPT> In fact, apart from using the "." reference attribute, we can also use the following statement:
Alert (userlist [0] ["name"] ["firstname"]); or alert (userlist [0]. name ["firstname"]);

Now, the reader should have some knowledge about the use of JSON, which is summarized as follows:
An object is a set of attributes and value pairs. An object starts with "{" and ends with "}". Use the ":" prompt for each attribute name and value, and separate the attributes.
An array is a set of ordered values. An array starts with "[", ends with "]", and values are separated.
The value can be a string, number, true, false, or null in quotation marks, or an object or array. These structures can be nested.
The definition of strings and numbers is basically the same as that of C or Java.

Http://www.json.org/json-zh.html

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.