我有一个像这样开始的json。我可以毫无问题地访问用户,声明和相关的第一级数据。我正在尝试在ClaimResponse中访问特定变量的数据(即User.id = 3)。我尝试了
x.ClaimResponse[Userid].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"
}
(...)
答案 0 :(得分:0)
[]运算符用于根据数组中的索引获取数据,例如,如果ClaimResponse [0]获得id为4的用户
答案 1 :(得分:0)
你可以在使用返回数组的关键ClaimResponse获取数据后为数组运行for循环。
喜欢
public static JSONObject ObjectWithIdandReponse(int _id,JSONArray responseArray) {
for (JSONObject Obj in responseArray)
{
if(Obj.id==_id)
return Obj;
}
}
Make方法返回JSON Obj并将参数作为用户ID和声明数组传递
答案 2 :(得分: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;
}
}