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:
- In different languages of the name/value pair set, it is understood as an object, record, structure, dictionary ), hash table and keyed list;
- 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, and 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, you can directly use any attribute 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 a little more complicated. For example, we define a more detailed structure for Name to make it have 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 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:
[{"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, in addition to using "." To reference attributes, 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.
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.