Working with complex JSON objects

Source: Internet
Author: User

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:

    1. $.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:

    1. {
    2. FirstName: ' Peggy ',
    3. LastName: ' Jones ',
    4. Phone: ' 415-555-5235 '
    5. }

The Web server may return a string representing a single JSON object, combining the two objects as follows:

    1. {
    2. contact1:{
    3. FirstName: ' Frank ',
    4. LastName: ' Smith ',
    5. Phone: ' 503-555-1212 '
    6. },
    7. contact2:{
    8. FirstName: ' Peggy ',
    9. LastName: ' Jones ',
    10. Phone: ' 415-555-5235 '
    11. }
    12. }

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:

    1. var data={
    2. contact1:{
    3. FirstName: ' Frank ',
    4. LastName: ' Smith ',
    5. Phone: ' 503-555-1212 '
    6. },
    7. contact2:{
    8. FirstName: ' Peggy ',
    9. LastName: ' Jones ',
    10. Phone: ' 415-555-5235 '
    11. }
    12. };

Now we can access the information for the first contact object in the callback function as follows:

    1. Data.contact1

and access the first contact name as follows:

    1. 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:

    1. $.each (JSON, function (name, value) {
    2. });

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. 1$.getjson (' contacts.php ', ' limit=2 ', processcontacts);
    2. 2 function processcontacts (data) {
    3. 3//create variable with empty string
    4. 4 var infohtml= ';
    5. 5
    6. 6//loop through each object in the JSON data
    7. 7$.each (data, function (contact, ContactInfo) {
    8. 8 infohtml+= '

      Contact: ' +contactinfo.frstname;

    9. 9 infohtml+= ' +contactinfo.lastname+ '
      ';
    10. Ten infohtml+= ' Phone: ' +contactinfo.phone+ '

      ';
    11. });//end of each ()
    12. 12
    13. 13//add finished HTML to page
    14. 14$ (' #info '). HTML (infohtml);
    15. 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:

    1. Contact:frank Smith

    2. phone:503-555-1212

    3. Contact:peggy Jones

    4. 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

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.