Javascript 当对象值是带有jQuery的对象数组时遍历JSON
以下是我的外部JSON:Javascript 当对象值是带有jQuery的对象数组时遍历JSON,javascript,jquery,json,Javascript,Jquery,Json,以下是我的外部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
{"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/"}]}
这是我的JS:
$.ajax({url: "/api/v1/entry/?format=json",
dataType: "json",
success: function(json) {
$.each(json.objects[0], function(key, value) {
alert(key + ': ' + value);
});
}
});
我可以使用$.each(json.objects[0])为数组中的对象编制索引,但我需要能够命中数组中的每个对象,我不知道为什么仅仅$.each(json.objects…)不起作用。谢谢!只需执行一个普通的JS循环:
for(var i = 0; i < json.objects.length; ++i)
{
$.each(json.objects[i], function(key, value) {
alert(key + ': ' + value);
});
}
for(var i=0;i
只需执行一个正常的JS循环:
for(var i = 0; i < json.objects.length; ++i)
{
$.each(json.objects[i], function(key, value) {
alert(key + ': ' + value);
});
}
for(var i=0;i
对于完整的jQuery解决方案,您可以执行以下操作:
$.each(json.objects, function(key, value) {
$.each(json.objects[key], function(key, value){
alert(key + ': ' + value);
})
});
jsiddle演示:注意,很多警报:p对于完整的jQuery解决方案,您可以执行以下操作:
$.each(json.objects, function(key, value) {
$.each(json.objects[key], function(key, value){
alert(key + ': ' + value);
})
});
JSFIDLE演示:当心,很多警报:p