所以我试图解析这个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 部分,该部分中的行数正确,并在表格视图中正确显示。然后在下一部分重复此操作。这需要适用于 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;
}
);