0

我正在尝试使用.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将它作为数组而不是对象来抓取。希望我忽略了一些简单的事情。想法?

4

2 回答 2

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);
      });
于 2013-05-10T03:05:52.490 回答
0

第一个索赔 ID 是

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

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

在此处输入图像描述

于 2013-05-10T03:06:35.577 回答