JSON is better than XML.

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 a subset of the JavaScript programming language in the ecma262 language specification (-12. 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.
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 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.
Section:

This article demonstrates how to use JSON through an example. It can be summarized as follows:

JSON provides an excellent object-oriented method to cache metadata on the client.
JSON helps to separate verification data and logic.
JSON helps provide the essence of Ajax for Web applications.
References:
Http://www.json.org/

Edit the recommended jquery article. Hope you like it:
JS excellent framework: latest version of jquery and official version of jquery
Http://www.xueit.com/html/2009-03/33_992_00.html
Automatically complete the plug-in using jquery like Google
Http://www.xueit.com/html/2009-03/33_874_00.html
The jquery + Ajax instance is used to query non-refreshing data (like Google/Baidu search box)
Http://www.xueit.com/html/2009-05/33_1796_00.html
Jquery tab plug-in recommendation: 17 compelling jquery navigation menus
Http://www.xueit.com/html/2009-09/2_4447_00.html
Download jquery plug-ins. There are 240 jquery plug-ins in total.
Http://www.xueit.com/html/2009-09/33_4506_00.html
Jquery imitation Baidu image magic light browsing Function
Http://www.xueit.com/html/2009-09/33_4573_00.html
Jquery voting plug-in: jquery plugin for displaying voting system results
Http://www.xueit.com/html/2009-11-10/33-1250974127921.html
20 super-cool jquery examples
Http://www.xueit.com/html/2009-11-25/33-1484914632500.html
Very nice webpage right-click menu, right-click menu implemented by jquery
Http://www.xueit.com/html/2009-10-30/33-19177319171.html
We recommend 10 jquery menu plug-ins with beautiful animated menus.
Http://www.xueit.com/html/2009-11-20/33-2102769523921.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.