0

在下面的数据示例中,我试图循环并从该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_id。

{
"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 的帮助。也许这更好地说明了我正在尝试做的事情。我想要与他们各自的声明相关的参与者图标。它很接近,但似乎从第一个索赔中吸引了所有参与者......:

http://jsfiddle.net/gkGP4/5/

4

4 回答 4

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);
      }
    });
  }
});
于 2013-07-14T14:34:33.417 回答
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
});

jsFiddle

于 2013-07-14T14:34:44.310 回答
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);

希望这可以帮助。

于 2013-07-14T18:28:50.047 回答
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 + '"/>');
});
});

http://jsfiddle.net/gkGP4/6/

感谢所有人提供的各种方法。

于 2013-07-15T03:39:02.863 回答