0

我有一个像这样开头的json。我可以毫无问题地访问 User、Claim 和相关的一级数据。我正在尝试访问 ClaimResponse 中特定变量的数据(即 User.id=3 的位置)。我已经尝试过

x.ClaimResponse[用户名].id
,没有运气。关于如何获取这些数据的任何直接想法?

{
"data": [
    {
        "User": {
            "score": "272",
            "id": "3"},
        "Claim": {
            "id": "2",
            "user_id": "3",
            "expiration": "2013-12-31 23:59:59",
            "editor_content": null,
            "point": "56.67",
            "claim_count": "3",
            "editor_pick": "0",
            "isCounterClaimed": "1",
            "headline": null,
            "subhead": null,
            "initial_point": "40",
            "claim_maker_cost": "60",
            "exp": "Dec 31, 2013"
        },
        "ClaimResponse": [
            {
                "id": "4",
                "claim_id": "2",
                "expiration": null,
                "response_type": "nay",
                "source": null,
                "created": "2013-05-15 03:28:18",
                "modified": "2013-05-15 03:28:18"
            },
            {
                "id": "12",
                "claim_id": "2",
                "expiration": null,
                "response_type": "yeh",
                "source": null,
                "created": "2013-05-15 14:47:30",
                "modified": "2013-05-15 14:47:30"
            },
            {
                "id": "13",
                "claim_id": "2",
                "expiration": null,
                "response_type": "nay",
                "source": null,
                "created": "2013-05-15 15:59:59",
                "modified": "2013-05-15 15:59:59"
            },
            {
                "id": "50",
                "claim_id": "2",
                "expiration": null,
                "response_type": "yeh",
                "source": null,
                "created": "2013-05-24 14:32:23",
                "modified": "2013-05-24 14:32:23"
            },
            {
                "id": "71",
                "claim_id": "2",
                "expiration": null,
                "response_type": "yeh",
                "source": null,
                "created": "2013-05-29 04:37:19",
                "modified": "2013-05-29 04:37:19"
            }
(...)
4

3 回答 3

0

您是否尝试获取具有特定 ID 的特定 ClaimResponse?如果是这样,试试这个:

var arr = x.ClaimResponse,
    userId = "3", //Set this to the uid you want
    length = arr.length,
    claim = null; //This variable stores the correct claim

for (var i = 0; i < length; i++) {
  claim = arr[i];
  if (claim.id === userId) {
    break;
  }
}
于 2013-05-30T05:18:26.397 回答
0

[] 运算符用于根据数组中的索引获取数据,例如,如果 ClaimResponse[0] 你得到 id 为 4 的用户

于 2013-05-30T05:07:54.470 回答
0

您可以在使用返回数组的键 ClaimResponse 获取数据后对得到的数组运行 for 循环。

喜欢

public static JSONObject ObjectWithIdandReponse(int _id,JSONArray responseArray) {
    for (JSONObject Obj in responseArray)
    {
     if(Obj.id==_id)
     return Obj;
    }
}

制作返回 JSON Obj 并将参数作为用户 ID 和声明数组传递的方法

于 2013-05-30T05:13:12.230 回答