当对象值是带有jQuery的对象数组时,遍历JSON [英] traverse JSON when object value is an array of objects with jQuery

查看:70
本文介绍了当对象值是带有jQuery的对象数组时,遍历JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的外部JSON:

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/"}]}

这是我的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 ...不起作用。谢谢!

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!

推荐答案

只需做一个普通的JS循环:

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); 
    });
}

这篇关于当对象值是带有jQuery的对象数组时,遍历JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆