1

Here is my external JSON:

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 3}, "objects": [{"body": "this is copy text", "id": "1", "pub_date": "2011-05-04T12:23:26", "resource_uri": "/api/v1/entry/1/", "slug": "test-title-number-one", "title": "test title number one", "user": "/api/v1/user/1/"}, {"body": "this is the second test text", "id": "2", "pub_date": "2011-05-04T15:01:16", "resource_uri": "/api/v1/entry/2/", "slug": "second-test", "title": "Second test", "user": "/api/v1/user/1/"}, {"body": "item three", "id": "3", "pub_date": "2011-05-05T12:04:04", "resource_uri": "/api/v1/entry/3/", "slug": "item-3", "title": "item 3", "user": "/api/v1/user/1/"}]}

Here is my JS:

$.ajax({url: "/api/v1/entry/?format=json", 
dataType: "json",
success: function(json) {
    $.each(json.objects[0], function(key, value) { 
      alert(key + ': ' + value); 
    });
}
});

I can index the objects in the array with $.each(json.objects[0]..., but I need to be able to hit each object in the array and I don't know why simply $.each(json.objects... doesn't work. Thanks!

2 Answers 2

6

Just do a normal JS loop:

for(var i = 0; i < json.objects.length; ++i)
{
   $.each(json.objects[i], function(key, value) { 
      alert(key + ': ' + value); 
    });
}
Sign up to request clarification or add additional context in comments.

Comments

2

For a full jQuery solution you can do:

$.each(json.objects, function(key, value) {
    $.each(json.objects[key], function(key, value){
        alert(key + ': ' + value);
    })
});

jsfiddle demo: http://jsfiddle.net/LGC9X/ beware, lots of alerts :P

Comments

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.