In step 4 of my Ajax, a new knowledge-JSON-is used to transmit the result from the server to the client. In fact, you can also seal the information of the corresponding result set to the XML document, and then return it to the client, and operate XML through DOM to get the corresponding results, but it seems very troublesome. As JSON is a new thing in the previous step, you still need to learn about JSON. I have found some materials and I will share with you an article that I think is good.
****************/
Author: Truly
JSON Definition
JSON (JavaScript Object Notation)
It 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 JavaScript in ecma262 language specification (-12 ).
A subset of programming languages. JSON uses a text format unrelated to the programming language, but also uses C-like languages (including C, C ++, C #, Java,
Javascript, Perl, Python, etc.), these features make JSON an ideal data exchange format.
The JSON structure is based on the following two points:
- 1. Set of "name/value" PairsIn different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list
- 2. ordered list of valuesMost languages are understood as arrays)
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 emailYou 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"]);
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.
***************** *****/