[英]traverse JSON when object value is an array of objects with jQuery
這是我的外部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/"}]}
這是我的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。對象...不起作用,謝謝!
只需執行一個普通的JS循環即可:
for(var i = 0; i < json.objects.length; ++i)
{
$.each(json.objects[i], function(key, value) {
alert(key + ': ' + value);
});
}
對於完整的jQuery解決方案,您可以執行以下操作:
$.each(json.objects, function(key, value) {
$.each(json.objects[key], function(key, value){
alert(key + ': ' + value);
})
});
jsfiddle演示: http : //jsfiddle.net/LGC9X/當心,很多警報:P
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.