What is JSON

Source: Internet
Author: User

Http://www.json.org/
IBM studio: http://www.ibm.com/developerworks/cn/web/wa-lo-json/
WIKI Encyclopedia: http://en.wikipedia.org/wiki/JSON
YAHOO zone: http://developer.yahoo.com/common/json.html

 

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.

 

Comparison between JSON and XML
◆ Readability
The readability of JSON and XML is comparable. One side is the suggested syntax and the other side is the standard tag format, which makes it difficult to distinguish between the two.
◆ Scalability
XML is inherently highly scalable, and JSON is also available. There is nothing XML can be extended, and JSON cannot.
◆ Encoding difficulty
XML has rich coding tools, such as Dom4j and JDom, and JSON also provides tools provided by json.org. However, JSON encoding is much easier than XML, JSON code can be written without tools, but it is not easy to write XML.
◆ Decoding difficulty
XML parsing takes into account the parent node of the child node, which is confusing, and the difficulty of JSON Parsing is almost 0. There is nothing to say about XML.
◆ Instance comparison
Both XML and JSON use structured methods to mark data. The following is a simple comparison.
Assume that a user data includes the user name, password, department, gender, and age.
It is represented as follows in XML:
<? Xml version = "1.0" encoding = "UTF-8"?>
<User>
<Name> Zhang San </name>
& Lt; password & gt; 123456 & lt;/password & gt;
<Department> Technical department </department>
<Sex> male </sex>
<Old> 30 </old>
</User>
JSON format:
{
"Name": "Zhang San ",
"Password": "123456 ",
"Department": "Technology department ",
"Sex": "male ",
"Old": 30
}
Like XML, JSON is also text-based and uses Unicode encoding, which is also readable. XML is more suitable for marking documents, while JSON is more suitable for data exchange processing.

 

JSON is constructed in two structures:

A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and 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 it possible to exchange a data format between programming languages that are also based on these structures.

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 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.

Spaces can be added to any symbol. The complete language is described below. (From: http://www.json.org/json-zh.html)

 

JSON example (json is used in javascript ):

The simplest one is:

<Script type = "text/javascript">
Var user = {"Id": 1, "Name": "Hubery", "Age": 23, "Email": "hubery@163.com "};
Alert (user. Id); // This access is OK. alert (user ["Id"]);
Alert (user. Name );
Alert (user. Age );
Alert (user. Email );
</Script>
We add the Address attribute to define a more detailed user information structure:

<Script type = "text/javascript">
Var user =
{
"Id": 1,
"Name": "Hubery ",
"Age": 23,
"Address ":
{
"City": "Beijing", "ZipCode": "111111"
},
"Email": "hubery@163.com"
};

Alert (user. Id );
Alert (user. Name );
Alert (user. Age );
Alert (user. Address. City );
Alert (user. Address. ZipCode );
Alert (user. Email );
</Script>
The Address attribute is defined as an array. The user has two addresses:

<Script type = "text/javascript">
Var user =
{
"Id": 1,
"Name": "Hubery ",
"Age": 23,
"Address ":
[
{"City": "Beijing", "ZipCode": "111111 "},
{"City": "Langfang", "ZipCode": "222222 "}
],
"Email": "hubery@163.com"
};

Alert (user. Id );
Alert (user. Name );
Alert (user. Age );
Alert (user. Address [0]. City); // You can also: alert (user. Address [0] ["City"]);
Alert (user. Address [0]. ZipCode );
Alert (user. Address [1]. City );
Alert (user. Address [1]. ZipCode );
Alert (user. Email );
</Script>
If we want a user list, can we? The answer is, no problem!

<Script type = "text/javascript">
Var user =
[
{
"Id": 1,
"Name": "Hubery ",
"Age": 23,
"Address ":
[
{"City": "Beijing", "ZipCode": "111111 "},
{"City": "Langfang", "ZipCode": "222222 "}
],
"Email": "hubery@163.com"
},
{
"Id": 2,
"Name": "Chris ",
"Age": 24,
"Address": {"City": "Beijing", "ZipCode": "100085 "},
"Email": "chris@163.com"
}
]

Alert ("Id:" + user [0]. Id + "\ r \ nName:" +
User [0]. Name + "\ r \ nAge:" +
User [0]. Age + "\ r \ nAddress: (" +
User [0]. address [0]. city + "," + user [0]. address [0]. zipCode + ") (" + user [0]. address [1]. city + "," + user [0]. address [1]. zipCode + ") \ r \ nEmail:" +
User [0]. Email );

Alert ("Id:" + user [1]. Id + "\ r \ nName:" +
User [1]. Name + "\ r \ nAge:" +
User [1]. Age + "\ r \ nAddress: (" +
User [1]. Address. City + "," + user [1]. Address. ZipCode + ") \ r \ nEmail:" +
User [1]. Email );
</Script>

 

Returns the top

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.