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