在下面的数据示例中,我正在尝试循环并从participants
部分获取数据。
通过ajax调用,我可以按如下方式访问其他数据,但我无法弄清楚这些数据的for语句。
var userData = $.ajax({
type: "POST",
url: userurl,
dataType: "json",
//data: { point: point, claimId: id, type: val,follow: followuser }
})
.success(function(json) {
$.each(json.data, function(i,x){
var id = x.Claim.user_id; // ETC
...
这很有效。使用此格式,我可以访问以下单个数据:x.participants[8].user_id
。我只是无法弄清楚如何循环participants
以获取所有参与者user_ids,例如。
{
"data": [
{
"Claim": {
"img_url": null,
"id": "4",
"user_id": "3",
"claim_status": "started",
"exp": "Sep 30, 2013"
},
"User": {
"score": "0",
"id": "3"
},
"ClaimResponse": [
{
"id": "32",
"claim_id": "4",
"user_id": "14",
"created": "2013-06-10 03:18:35",
"modified": "2013-06-10 03:18:35"
},
{
"id": "107",
"claim_id": "4",
"user_id": "8",
"created": "2013-06-28 02:37:10",
"modified": "2013-06-28 02:37:10"
},
{
"id": "140",
"claim_id": "4",
"user_id": "976",
"created": "2013-06-28 04:19:22",
"modified": "2013-06-28 04:19:22"
},
{
"id": "152",
"claim_id": "4",
"user_id": "19",
"created": "2013-07-03 02:27:12",
"modified": "2013-07-03 02:27:12"
},
{
"id": "154",
"claim_id": "4",
"user_id": "1158",
"created": "2013-07-05 03:16:48",
"modified": "2013-07-05 03:16:48"
}
],
"participants": {
"14": {
"user_id": "14",
"twitter_image_url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png"
},
"8": {
"user_id": "8",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_3_normal.png"
},
"19": {
"user_id": "19",
"twitter_image_url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
},
"1158": {
"user_id": "1158",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_5_normal.png"
}
},
"viewer": {
"isResponsed": false,
"isOwner": false,
"lockedInPoint": 0,
"response": null,
"cut_loss": false,
"cut_loss_price": null
}
},
{
"Claim": {
"img_url": null,
"id": "133",
"user_id": "14",
"claim_status": "started",
"exp": "Jul 15, 2013"
},
"User": {
"score": "-40",
"id": "14"
},
"ClaimResponse": [
{
"id": "172",
"claim_id": "133",
"user_id": "8",
"created": "2013-07-12 20:36:16",
"modified": "2013-07-12 20:36:16"
},
{
"id": "176",
"claim_id": "133",
"user_id": "1159",
"created": "2013-07-13 02:52:31",
"modified": "2013-07-13 02:52:31"
}
],
"participants": {
"8": {
"user_id": "8",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_5_normal.png"
},
"1159": {
"user_id": "1159",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_5_normal.png"
}
},
"viewer": {
"isResponsed": false,
"isOwner": false,
"lockedInPoint": 0,
"response": null,
"cut_loss": false,
"cut_loss_price": null
}
}
],
"errors": [],
"success": true,
"code": 200
}
编辑:小提琴,感谢@ohgodwhy的帮助。也许这更好地说明了我想要做的事情。我希望参与者图标与他们各自的声明相关联。它很接近,但似乎从第一个索赔中吸引了所有参与者......:
答案 0 :(得分:0)
data
数组包含一个包含所有其他数据的对象,因此您应该遍历json.data[0]
而不是json.data
。
由于混合了直接包含数据的对象,包含对象的数组和包含对象的对象,您必须以不同的方式遍历项目以查找属性:
var ids = [];
$.each(json.data[0], function(i, x) {
if (x.hasOwnProperty('user_id') {
ids.push(x.user_id);
} else {
$.each(x, function(j, y) {
if (y.hasOwnProperty('user_id') {
ids.push(y.user_id);
}
});
}
});
答案 1 :(得分:0)
您必须在返回的JSON中选择数据对象的索引。然后,一旦你有了,你可以迭代参与者。这是因为返回时data
是array
,因此我们需要选择对象所在数据的索引。如果你不提供它,你将获得undefined
。
$.each(json.data[0].participants, function(i,p){
//access p with properties
console.log(p.twitter_image_url); //twitter image url
console.log(p.user_id); //user's id
});
答案 2 :(得分:0)
如果您要查找包含claimid和相关参与者ID的结果集
var result = $.map(response.data, function(item){
return { ClaimId : item.Claim.id, Participants: $.map(item.participants, function(item1){ return item1.user_id; }) };
});
console.log(result);
希望这会有所帮助。
答案 3 :(得分:0)
问题当然是我的循环所在。我过早地循环并且调用所有我最初迭代的变量d次(d是我的嵌套循环中的项目数)。
以下是解决方案:
var d = {
"data": [{
"Claim": {
"img_url": null,
"id": "4",
"user_id": "3",
"claim_status": "started",
"exp": "Sep 30, 2013"
},
"User": {
"score": "0",
"id": "3"
},
"ClaimResponse": [{
"id": "32",
"claim_id": "4",
"user_id": "14",
"created": "2013-06-10 03:18:35",
"modified": "2013-06-10 03:18:35"
}, {
"id": "107",
"claim_id": "4",
"user_id": "8",
"created": "2013-06-28 02:37:10",
"modified": "2013-06-28 02:37:10"
}, {
"id": "140",
"claim_id": "4",
"user_id": "976",
"created": "2013-06-28 04:19:22",
"modified": "2013-06-28 04:19:22"
}, {
"id": "152",
"claim_id": "4",
"user_id": "19",
"created": "2013-07-03 02:27:12",
"modified": "2013-07-03 02:27:12"
}, {
"id": "154",
"claim_id": "4",
"user_id": "1158",
"created": "2013-07-05 03:16:48",
"modified": "2013-07-05 03:16:48"
}],
"participants": {
"14": {
"user_id": "14",
"twitter_image_url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png"
},
"8": {
"user_id": "8",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_3_normal.png"
},
"19": {
"user_id": "19",
"twitter_image_url": "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png"
},
"1158": {
"user_id": "1158",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_5_normal.png"
}
},
"viewer": {
"isResponsed": false,
"isOwner": false,
"lockedInPoint": 0,
"response": null,
"cut_loss": false,
"cut_loss_price": null
}
}, {
"Claim": {
"img_url": null,
"id": "133",
"user_id": "14",
"claim_status": "started",
"exp": "Jul 15, 2013"
},
"User": {
"score": "-40",
"id": "14"
},
"ClaimResponse": [{
"id": "172",
"claim_id": "133",
"user_id": "8",
"created": "2013-07-12 20:36:16",
"modified": "2013-07-12 20:36:16"
}, {
"id": "176",
"claim_id": "133",
"user_id": "1159",
"created": "2013-07-13 02:52:31",
"modified": "2013-07-13 02:52:31"
}],
"participants": {
"8": {
"user_id": "8",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_5_normal.png"
},
"1159": {
"user_id": "1159",
"twitter_image_url": "https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_5_normal.png"
}
},
"viewer": {
"isResponsed": false,
"isOwner": false,
"lockedInPoint": 0,
"response": null,
"cut_loss": false,
"cut_loss_price": null
}
}],
"errors": [],
"success": true,
"code": 200
}
$.each(d.data, function (i, x) {
id = x.Claim.id;
$('body').append('<div class="id"><p id="img'+x.Claim.id+'">Claim' + x.Claim.id + '</p></div>');
$.each(x.participants, function (i, p) {
$('#img'+x.Claim.id).append('<img src="' + p.twitter_image_url + '"/>');
});
});
感谢所有各种方法。