0

I am retrieving a multidimensional JSON array using JQUERY.

I need to print out various items from the array, but am having a hard time figuring out how to go through the array and get these items so I can insert them into the HTML.

Here is an example of the array (this is what is taken from the jsonpage.php referenced below.

{
   "count":1,
   "total_count":1,
   "contacts":[
      {
         "id":92840643,
         "user_id":55536,
         "first_name":"John",
         "last_name":"Doe",
         "full_name":"John  Doe",
         "initials":"JD",
         "title":null,
         "company":null,
         "email":"[email protected]",
         "avatar":"https://graph.facebook.com/123454/picture?type=large",
         "avatar_url":"https://graph.facebook.com/123454/picture?type=large",
         "last_contacted":null,
         "visible":true,
         "twitter":null,
         "facebook_url":null,
         "linkedin_url":null,
         "first_contacted":null,
         "created_at":"2014-05-26T19:06:55Z",
         "updated_at":"2014-05-26T19:12:42Z",
         "hits":0,
         "user_bucket_id":486405,
         "team_parent_id":null,
         "snoozed_at":null,
         "snooze_days":null,
         "groupings":[
            {
               "id":21554286,
               "type":"Grouping::Location",
               "name":"Johnson, NY",
               "stub":"frisco tx",
               "bucket_id":null,
               "user_id":55536,
               "domain_id":null,
               "editable":null,
               "conversable":null,
               "locked":null,
               "derived_from_id":null
            },
            {
               "id":21553660,
               "type":"Grouping::Bucket",
               "name":"Top Customers",
               "stub":"top customers",
               "bucket_id":486405,
               "user_id":55536,
               "domain_id":null,
               "editable":null,
               "conversable":null,
               "locked":null,
               "derived_from_id":null,
               "has_followups":true,
               "num_days_to_followup":30,
               "program_id":null
            }
         ],
         "email_addresses":[
            "[email protected]"
         ],
         "tags":[

         ],
         "contact_status":3,
         "team_last_contacted":null,
         "team_last_contacted_by":null,
         "phone_numbers":[

         ],
         "addresses":[
            {
               "_id":"538390cfcc0fb067d8000353",
               "created_at":"2014-05-26T19:06:55Z",
               "deleted_at":null,
               "extra_data":{
                  "address_city":"Johnson",
                  "address_state":"NY",
                  "address_country":"United States"
               },
               "label":"Address",
               "primary":null,
               "remote_id":null,
               "updated_at":"2014-05-26T19:06:55Z",
               "username":null,
               "value":"Johnson, NY\nUnited States"
            }
         ],
         "social_profiles":[

         ],
         "websites":[

         ],
         "custom_fields":[
            {
               "_id":"538390cfcc0fb067d8000354",
               "custom_field_id":46639,
               "deleted_at":null,
               "label":"WeeklyNews",
               "value":"YES"
            },
            {
               "_id":"538390cfcc0fb067d8000355",
               "custom_field_id":46640,
               "deleted_at":null,
               "label":"Current Credits",
               "value":"142"
            },
            {
               "_id":"538390cfcc0fb067d8000356",
               "custom_field_id":46641,
               "deleted_at":null,
               "label":"Total Purchased Amount",
               "value":"400"
            },
            {
               "_id":"538390cfcc0fb067d8000357",
               "custom_field_id":46642,
               "deleted_at":null,
               "label":"VDownloads",
               "value":"112"
            },
            {
               "_id":"538390cfcc0fb067d8000358",
               "custom_field_id":46643,
               "deleted_at":null,
               "label":"AEDownloads",
               "value":"9"
            },
            {
               "_id":"538390cfcc0fb067d8000359",
               "custom_field_id":46644,
               "deleted_at":null,
               "label":"ADownloads",
               "value":"53"
            },
            {
               "_id":"538390cfcc0fb067d800035a",
               "custom_field_id":46638,
               "deleted_at":null,
               "label":"Last Login",
               "value":"2014-05-25 23:14:19"
            },
            {
               "_id":"538390cfcc0fb067d800035b",
               "custom_field_id":46649,
               "deleted_at":null,
               "label":"Label",
               "value":"Group1"
            }
         ]
      }
   ]
}

And here is the jquery success code:

 $.post('/jsonpage.php', post_data,  function(response) {



 });

Now, if I put alert(response); within the function i.e.:

 $.post('/jsonpage.php', post_data,  function(response) {

 alert(response);
      });

Then, it 'does' alert the entire JSON string as listed above.

However, if I put this:

 $.post('/jsonpage.php', post_data,  function(response) {

 alert(response.count);
      });

Then, I get UNDEFINED in the alert box. I have tried a few different variables to 'alert' and they all come back undefined.

Thanks for your help!

Craig

3
  • Unless your html page is very complex and you really need everything, imo there is way too much "Stuff" in that json and jsonpage.php should probably trim down that massive blob to something more useful/manageable/not as bandwidth intensive. But hey, maybe you need it all! Commented Jun 28, 2014 at 20:07
  • Thanks James. I am retrieving it via an API, and you are right. I don't need it all and I will see how I can limit it down and not get everything back. Thanks for the comment. Commented Jun 28, 2014 at 20:24
  • Thanks for the edit and cleaning up my question as well. I very much appreciate that. Commented Jun 28, 2014 at 20:37

3 Answers 3

1
response.total_count
response.contacts[0].id
response.contacts[0].groupings[0].stub

And so on.

Sign up to request clarification or add additional context in comments.

8 Comments

response is data, not a DOM element
Oh, sorry, typo. Edited.
This looks right, but I am getting UNDEFINED when trying to alert my variables. Can you check my edits above?
Try to inspect the response in the Google Developer Tools console, i.e. do a console.log(response) and expand the result.
Great! Have a nice day.
|
1

Here are some ways of using the data in your json response. Hope it helps.

$.post('/jsonpage.php', post_data,  function(response) {

  if (!response.contacts || !response.contacts.length) {
    alert("Error loading that/those contact(s)");
    return;
  }

  for (var i=0, c; c = response.contacts[i]; i++) {
    // c is each contact, do stuff with c
    alert("That contact was created at " + c.created_at + " and last updated at " + c.updated_at);

    var cities = [];

    for (var j=0, a; a = c.addresses[j]; j++) {
      // a refers to each address
      cities.push(a.extra_data.address_city);
    }
    alert(c.full_name + " lives in " + cities.join(" and ") + ".");

    for (var j=0, cf; cf = c.custom_fields[j]; j++) {
      // cf is each custom_field
      // build a form or something
      // element label is cf.label
      // element value is currently cf.value
      var p = document.createElement("p");
      p.appendChild(document.createTextNode(cf.label));

      var el = document.createElement("input");
      el.type = "text";
      el.value = cf.value;

      p.appendChild(el);

      document.getElementById("someForm").appendChild(p);

    }

  }

});

1 Comment

Checking this out now James, but thanks for taking the time to post this extra info./details. Awesomeness!
0

Try this

var data = { "count": 1, "total_count": 1, "contacts": [{ "id": 92840643, "user_id": 55536, "first_name": "John", "last_name": "Doe", "full_name": "John  Doe", "initials": "JD", "title": null, "company": null, "email": "[email protected]", "avatar": "https://graph.facebook.com/123454/picture?type=large", "avatar_url": "https://graph.facebook.com/123454/picture?type=large", "last_contacted": null, "visible": true, "twitter": null, "facebook_url": null, "linkedin_url": null, "first_contacted": null, "created_at": "2014-05-26T19:06:55Z", "updated_at": "2014-05-26T19:12:42Z", "hits": 0, "user_bucket_id": 486405, "team_parent_id": null, "snoozed_at": null, "snooze_days": null, "groupings": [{ "id": 21554286, "type": "Grouping::Location", "name": "Johnson, NY", "stub": "frisco tx", "bucket_id": null, "user_id": 55536, "domain_id": null, "editable": null, "conversable": null, "locked": null, "derived_from_id": null }, { "id": 21553660, "type": "Grouping::Bucket", "name": "Top Customers", "stub": "top customers", "bucket_id": 486405, "user_id": 55536, "domain_id": null, "editable": null, "conversable": null, "locked": null, "derived_from_id": null, "has_followups": true, "num_days_to_followup": 30, "program_id": null}], "email_addresses": ["[email protected]"], "tags": [], "contact_status": 3, "team_last_contacted": null, "team_last_contacted_by": null, "phone_numbers": [], "addresses": [{ "_id": "538390cfcc0fb067d8000353", "created_at": "2014-05-26T19:06:55Z", "deleted_at": null, "extra_data": { "address_city": "Johnson", "address_state": "NY", "address_country": "United States" }, "label": "Address", "primary": null, "remote_id": null, "updated_at": "2014-05-26T19:06:55Z", "username": null, "value": "Johnson, NY\nUnited States"}], "social_profiles": [], "websites": [], "custom_fields": [{ "_id": "538390cfcc0fb067d8000354", "custom_field_id": 46639, "deleted_at": null, "label": "WeeklyNews", "value": "YES" }, { "_id": "538390cfcc0fb067d8000355", "custom_field_id": 46640, "deleted_at": null, "label": "Current Credits", "value": "142" }, { "_id": "538390cfcc0fb067d8000356", "custom_field_id": 46641, "deleted_at": null, "label": "Total Purchased Amount", "value": "400" }, { "_id": "538390cfcc0fb067d8000357", "custom_field_id": 46642, "deleted_at": null, "label": "VDownloads", "value": "112" }, { "_id": "538390cfcc0fb067d8000358", "custom_field_id": 46643, "deleted_at": null, "label": "AEDownloads", "value": "9" }, { "_id": "538390cfcc0fb067d8000359", "custom_field_id": 46644, "deleted_at": null, "label": "ADownloads", "value": "53" }, { "_id": "538390cfcc0fb067d800035a", "custom_field_id": 46638, "deleted_at": null, "label": "Last Login", "value": "2014-05-25 23:14:19" }, { "_id": "538390cfcc0fb067d800035b", "custom_field_id": 46649, "deleted_at": null, "label": "Label", "value": "Group1"}]}] };
    alert(data["contacts"][0]["id"]);

//get count
alert(data["count"]); //1
//get first contacts data
data["contacts"][0]["id"] //retruns 92840643
//do same for others to get data

1 Comment

I think you've got a typo there: alert(j["contacts"][0]["id"]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.