Json+javascript A simple example of processing JSON _javascript tips

Source: Internet
Author: User

JSON (JavaScript Object notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy to machine parse and generate. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON uses a completely language-independent text format, but it also uses a family of C-language (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:

1, the collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).

2, the value of the sequence table (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 way. This makes it possible for a data format to be exchanged between programming languages that are also based on these constructs.

JSON has the following forms:

• The object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma).
• An array is an ordered collection of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value.
• values (value) can be enclosed in double quotes (string), numeric (number), True, False, NULL, objects (object), or arrays (array). These structures can be nested.
• A string is a collection of any number of Unicode characters surrounded by double quotes that are escaped using backslashes. A character (character) is a separate string (character string).
• A string is very similar to a C or Java string.
numeric values (number) are also similar to those of C or Java. Remove unused octal and hexadecimal formatting. Remove some coding details.
• Whitespace can be added to any symbol. The complete language is described below.
JSON examples (using JSON in JavaScript):

Copy Code code as follows:

<script type= "Text/javascript" >
var user =
{
"Id": 1,
"Name": "Hubery",
"Age": 23,
"Address":
{
"City": "Beijing", "ZipCode": "111111"
},
"Email": "Hubery@jb51.net"
};

Alert (user. ID);
Alert (user. Name);
Alert (user. Age);
Alert (user. address.city);
Alert (user. Address.zipcode);
Alert (user. Email);
</script>
Here we define the address attribute as an array with two address users:

<script type= "Text/javascript" >
var user =
{
"Id": 1,
"Name": "Hubery",
"Age": 23,
"Address":
[
{"City": "Beijing", "ZipCode": "111111"},
{"City": "Langfang", "ZipCode": "222222"}
],
"Email": "Hubery@jb51.net"
};

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 do it? The answer is no problem!

Copy Code code as follows:

<script type= "Text/javascript" >
var user =
[
{
"Id": 1,
"Name": "Hubery",
"Age": 23,
"Address":
[
{"City": "Beijing", "ZipCode": "111111"},
{"City": "Langfang", "ZipCode": "222222"}
],
"Email": "Hubery@jb51.net"
},
{
"Id": 2,
"Name": "Chris",
"Age": 24,
"Address": {"City": "Beijing", "ZipCode": "100085"},
"Email": "Chris@jb51.net"
}
]

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>

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.