getJSON()访问多个级别的数据

时间:2013-05-10 03:01:36

标签: ajax json getjson

我正在尝试使用.getJSON从此验证.json中提取数据:

{
    "data": [
        {
            "Claim": {
                "id": "3567",
                "user_id": "341",
                "expiration": "2013-07-06 23:59:59",
                "content": "Apples",
                "point": "85.71",
                "exp": "Jul 6, 2013"
            },
            "User": {
                 "score": "-1030.17",
                "id": "341"
            },
            "ClaimResponse": {
                "point": "80",
                "response_type": "yeah",
                "claim_id": "3567"
            },
            "viewer": {
                "isOwner": "0",
                "isResponsed": "1",
                "response": "yeah",
                "lockedInPoint": "80"
            }
        },
        {
            "Claim": {
                "id": "3576",
                "user_id": "342",
                "expiration": "2013-06-15 00:00:00",
                "content": "Oranges",
                "point": "86.42",
                "exp": "Jun 15, 2013"
            },
            "User": {
                "score": "-3128.13",
                "id": "342"
            },
            "ClaimResponse": {
                "point": "71",
                "response_type": "yeah",
                "claim_id": "3576"
            },
            "viewer": {
                "isOwner": "0",
                "isResponsed": "1",
                "response": "yeah",
                "lockedInPoint": "71"
            }
        },
        {
            "Claim": {
                "id": "3577",
                "user_id": "342",
                "expiration": "2013-07-01 00:00:00",
                "content": "Peanuts",
                "point": "80.25",
                "exp": "Jul 1, 2013"
            },
            "User": {
                "score": "-3128.13",
                "id": "342"
            },
            "ClaimResponse": {
                "point": "67",
                "response_type": "yeah",
                "claim_id": "3577"
            },
            "viewer": {
                "isOwner": "0",
                "isResponsed": "1",
                "response": "yeah",
                "lockedInPoint": "67"
            }
        }
    ],
    "errors": [],
    "success": true,
    "code": 200
}

(许多记录的片段)我从来没有试图像这样提取多个级别的数据,我无法理解它。

我的代码很简单:

$.getJSON("/api/getUserClaims.json",function(data){

          $.each(data.Claim, function(i,Claim){
            content = '<p>' + Claim.id + '</p>';
            $(content).appendTo("#my-claims");
            alert(content);
          });
      });

根据我的控制台,json正在正确加载。我尝试了几种方法,使用data.Claim[0].id将其作为数组而不是对象来获取。希望我忽略了一些简单的事情。想法?

2 个答案:

答案 0 :(得分:2)

没有data.Claim

你首先有一个数组,所以它是data[0].Claimdata[1].Claim等。

$.each(data, function(i,x){
        var Claim = x.Claim;
        content = '<p>' + Claim.id + '</p>';
        $(content).appendTo("#my-claims");
        alert(content);
      });

答案 1 :(得分:0)

第一个索赔ID是

data[0]["Claim"]["id"]

数据是一个对象数组。要更好地将其可视化,请使用Chrome或Firefox开发人员工具。您可以浏览您的数据,然后突然变得更加清晰:

enter image description here