Reprint Please specify source: http://www.uphtm.com/js/226.html
We can use the object direct amount as a value in a JSON object, in other words, the direct amount of the object is nested in the direct amount of the object, creating even more complex collections of information.
Here's an example. Let's say we want the server to send back more personal contact information using JSON. We will send a request to a file named contacts.php with a query string indicating how many contacts you want to return. The code might look like this:
- $.getjson (' contacts.php ', ' limit=2 ', processcontacts);
Limit=2 is the information that is sent to the server, which indicates how many contacts should be returned. The Web server will then return information for two contact persons. Assuming the first person's contact information is the same as the example above (Frank Smith), the second person's contact information is another JSON object as shown below:
- {
- FirstName: ' Peggy ',
- LastName: ' Jones ',
- Phone: ' 415-555-5235 '
- }
The Web server may return a string representing a single JSON object, combining the two objects as follows:
- {
- contact1:{
- FirstName: ' Frank ',
- LastName: ' Smith ',
- Phone: ' 503-555-1212 '
- },
- contact2:{
- FirstName: ' Peggy ',
- LastName: ' Jones ',
- Phone: ' 415-555-5235 '
- }
- }
Assume that the callback function receives a single parameter named data (for example, function processcontacts-(data)). The variable data is then assigned to the JSON object, as if the following code was executed:
- var data={
- contact1:{
- FirstName: ' Frank ',
- LastName: ' Smith ',
- Phone: ' 503-555-1212 '
- },
- contact2:{
- FirstName: ' Peggy ',
- LastName: ' Jones ',
- Phone: ' 415-555-5235 '
- }
- };
Now we can access the information for the first contact object in the callback function as follows:
- Data.contact1
and access the first contact name as follows:
- Data.contact1.firstName
However, in this case, because we want to handle multiple contacts, jquery provides a function that allows you to iterate through each item in the JSON object, which is the each () function. Its basic structure is:
- $.each (JSON, function (name, value) {
- });
We pass the JSON object and an anonymous function to each () function. This anonymous function receives the name and value of each item in the JSON object. Here's what the JSON object looks like in the current example:
- 1$.getjson (' contacts.php ', ' limit=2 ', processcontacts);
- 2 function processcontacts (data) {
- 3//create variable with empty string
- 4 var infohtml= ';
- 5
- 6//loop through each object in the JSON data
- 7$.each (data, function (contact, ContactInfo) {
- 8 infohtml+= '
Contact: ' +contactinfo.frstname;
- 9 infohtml+= ' +contactinfo.lastname+ '
';
- Ten infohtml+= ' Phone: ' +contactinfo.phone+ '
';
- });//end of each ()
- 12
- 13//add finished HTML to page
- 14$ (' #info '). HTML (infohtml);
- 15}
The code decomposition analysis is as follows:
1. Line 1th creates an AJAX request and specifies a callback function.
2. Line 2nd creates a callback function that receives the JSON object sent back by the server and stores it in the variable data.
3. Line 4th creates an empty string. The HTML that is eventually added to the page populates the string.
4. Line 7th is the each () function, which iterates through the objects in the JSON data.
each () function receives a JSON object as its first parameter (data) and receives an anonymous function as its second argument. Basically, for each object in the main object (in this case, Contact1 and Contact2), the anonymous function receives the name of the object as a string (that is, the contact parameter listed in line 7th) and the value of the object (that is, the ContactInfo parameter). In this example, the ContactInfo variable will hold the direct amount of the object that contains the contact information.
5. The 8th to 10th line extracts the information from a contact person.
The each () function is a loop, so the 8th to 10th Line runs two times for each contact.
6. Line 14th updates the Web page by adding HTML to the page.
The final result is the following HTML:
-
Contact:frank Smith
- phone:503-555-1212
-
Contact:peggy Jones
- phone:415-555-5235
Original: Processing complex JSON objects, reproduced please specify the source: http://www.uphtm.com/js/226.html
Working with complex JSON objects