JSON learning notes

Source: Internet
Author: User
JSON is coming. JSON is coming.

The xmlHttpRequest component is used as the interaction tool and XML as the data transmission format.

[XML]
Advantages of using XML as the transmission format:
1. unified format, compliant with standards
2. Easy remote interaction with other systems and convenient data sharing

Disadvantages:
1. XML file format is large, complex, and occupies bandwidth for Transmission
2. Both the server and client require a large amount of code to parse XML, regardless of the complexity and difficulty of maintaining the server and client code.
3. The method for parsing XML between different browsers on the client is inconsistent. You need to repeat a lot of code.
4. It takes resources and time for the server and client to parse XML

[JSON]
In addition to the XML format, there are no other formats. There is a lightweight data exchange format called JSON (JavaScript Object Notation) that can replace XML.

Advantages:
1. The data format is relatively simple, easy to read and write, the format is compressed, and the bandwidth usage is small
2. It is easy to parse this language. The client JavaScript can simply read JSON data through eval ().
3. Supports multiple languages, including ActionScript, C, C #, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby, and other language server-side languages for server-side Parsing
4. in the PHP world, there has been a PHP-JSON and a JSON-PHP that facilitates PHP Serialized programs to call directly. PHP server objects and arrays can directly generate JSON format to facilitate client access extraction.
In addition, the PEAR class of PHP has been supported (#)
5. JSON format can be used directly for server-side code, which greatly simplifies the development of server-side and client-side code, but the completed tasks remain unchanged and easy to maintain.

Disadvantages:
1. It is widely used and widely used without the XML format. It is not as universal as XML.
2. The JSON format is still in the initial stage in Web Service promotion.

PS: It is said that Google's Ajax is made using the JSON + template.

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:
JavaScript code
{"UserID": 11, "Name": "Truly", "Email": "zhuleipro ◎ hotmail.com "};
If you assign this string to a JavaScript variable, you can directly use any attribute of the object.
Complete code:
JavaScript code

《script》 var User = {"UserID":11, "Name":"Truly", "Email":"zhuleipro◎hotmail.com"}; alert(User.Name); 《script》

In actual use, it may be a little more complicated. For example, we define a more detailed structure for Name to make it have FirstName and LastName:
JavaScript code
{"UserID": 11, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "zhuleipro ◎ hotmail.com "}
Complete code:
JavaScript code

《script》 var User = {"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"}; alert(User.Name.FirstName); 《script》


Now we want to add a new requirement. A page requires a user list, not just a single user information. here we need to create an array of user lists.
The following code defines the user list in JSON format:
JavaScript code

[ {"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:
JavaScript 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, in addition to using "." To reference attributes, we can also use the following statement:
JavaScript code

The Code is as follows:

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.

Related Article

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.