所以我试图解析这个json数据:
{
deal = Test;
expires = "2012-07-29 22:50:30";
"is_active" = 1;
"id" = 27;
users = (
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 34;
},
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 35;
},
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 36;
},
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 38;
}
);
},
{
deal = Testing;
expires = "2012-07-29 23:37:38";
"is_active" = 0;
"id" = 26;
users = (
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 37;
}
);
}
所以我想要处理每个部分的标题,然后将行作为与该交易相关联的每个用户。因此,对于第一笔交易“测试”,将有四行用户34,35,36和38.然后对于第二笔交易“测试”,只有一个用户,因此只有一行。这需要能够为无限数量的交易工作。我可以将所有交易解析为一个数组,但我无法弄清楚如何分离用户。请帮忙!
编辑:
这就是我获取Json数据并存储它和交易的方式。
NSURL *loginURL = [NSURL URLWithString: [NSString stringWithFormat: URL];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:loginURL];
[theRequest setHTTPMethod:@"POST"];
NSError *error;
NSURLResponse *response;
NSData * responseData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSDictionary * luckyNumbers = [NSJSONSerialization JSONObjectWithData:responseData //1
options:NSJSONReadingAllowFragments
error:&error];
NSLog(@" %@", luckyNumbers);
NSArray * array1 = [luckyNumbers objectForKey:@"active"]; //THIS IS THE JSON THAT IS DISPLAYED ABOVE
deals = [[NSMutableArray alloc] init];
active = [[NSMutableArray alloc] init];
for (NSDictionary *item in array1)
{
[deals addObject:item]; // THIS WORKS FINE. IT RETURNS ALL OF THE DEALS IN AN ARRAY WHICH I SET TO THE NUMBER OF SECTIONS IN TABLE AND THE HEADER TITLE OF THE SECTION
[active addObject:[item objectForKey:@"users"]];//THIS IS THE PROBLEM.
}
编辑2:
所以这仍然给我带来了问题。我能够完美地解析交易,并将它们显示为每个部分的标题。当我将用户解析为一个名为active的数组时它是一个数组,如下所示。前四个是交易“测试”的一部分,最后一个是交易“测试”的一部分。我不确定如何将前四个用户设置在第0部分,在该部分中使用正确的行数并在tableview中正确显示它们。然后在下一节重复此操作。这需要适用于Y个用户的X个交易。我希望这是有道理的,请帮忙!
(
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 34;
},
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 35;
},
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 36;
},
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 38;
}
),
(
{
distance = 0;
latitude = "41.312618";
longitude = "-74.195114";
"user_id" = 37;
}
);
答案 0 :(得分:0)
var yourDataHolder = JSON.parse(yourJSONObject);
for(var x in yourDataHolder)
{
if(yourDataHolder[x].hasOwnProperty)
{
/**
* DO STUFF HERE
*/
console.log(yourDataHolder[x].deal);
console.log(yourDataHolder[x].expires);
}
}
为了让您更容易理解这里发生的事情:
yourDataHolder [x]将迭代JSON对象中的不同项。
要访问对象的属性,请使用yourDataHolder[x].deal,yourDataHolder[x].expires,yourDataHolder[x].is_active,yourDataHolder[x].id
要访问用户表中的数据,请执行以下操作: var yourDataHolder = JSON.parse(yourJSONObject);
for(var x in yourDataHolder)
{
if(yourDataHolder[x].hasOwnProperty)
{
/**
* DO STUFF HERE
*/
for(var y in yourDataHolder[x].users)
{
if(yourDataHolder[x].users[y].hasOwnProperty)
{
/**
* You are now inside the current "users" object
*/
console.log(yourDataHolder[x].users[y]);
}
}
}
}