Processing complex JSON objects and processing complex JSON objects
Reprinted please indicate the source: http://www.uphtm.com/js/226.html
We can use the direct object quantity as the value in a JSON object. In other words, the object quantity is nested in the direct object quantity to create even more complex information sets.
Here is an example. Suppose we want the server to send back contact information of multiple people in JSON format. We will send a request to the file named contacts. php with a query string indicating the number of contacts to be returned. The code may be as follows:
Limit = 2 is the message sent to the server, which indicates the number of contacts that should be returned. The Web server then returns the information of two contacts. Assume that the contact information of the first person is the same as that of Frank Smith in the preceding example. The contact information of the second person is another JSON object shown below:
The Web server may return a string that represents a single JSON object. It combines these two objects, as shown below:
Assume that the callback function receives a single parameter named data (for example, processContacts-(data )). The variable data will be subsequently allocated to the JSON object, as if the following code is executed:
Now, we can access the information of the first contact object in the callback function, as shown below:
And access the name of the first contact as follows:
However, in this example, because we want to process multiple contacts, jQuery provides a function that allows every project in the JSON object to be traversed. This is the each () function. Its basic structure is:
We passed the JSON object and an anonymous function to the each () function. This anonymous function receives the name and value of each project in the JSON object. The following describes how to use a JSON object in the current example:
Code decomposition analysis is as follows:
1. the Ajax request is created in Row 3 and the callback function is specified.
2. a callback function is created in row 2nd. It receives the JSON object sent back by the server and stores it in variable data.
3. An empty string is created in row 4th. The HTML added to the page will fill in the string.
4. Row 3 is the each () function, which traverses objects in JSON data.
The each () function receives a JSON object as its first parameter (data) and an anonymous function as its second parameter. Basically, for each object in the main object (contact1 and contact2 in this example ), the anonymous function receives the name of the object as a string (that is, the contact parameter listed in row 7th) and the value of the object (that is, the contactInfo parameter ). In this example, the contactInfo variable stores the direct quantity of objects containing contact information.
5. 8th ~ 10 rows extract information from a contact.
The each () function is a loop. Therefore, 8th ~ Line 10 runs twice for each contact.
14th rows update the Web page by adding HTML to the page.
The final result is the following HTML:
Original: processing complex JSON object, reprinted please note the Source: http://www.uphtm.com/js/226.html