我有一个json数组,如下所示:
a= {"title":"workers","data":[{"name":"tom","id":"LBJP01Z"},{"name":"bob","id":"LBJP08Z"},{"name":"bill","id":"LBJP02Z"}]},{"title":"teachers","data":[{"name":"jill","id":"LZJP01Z"},{"name":"tim","id":"LBJP03Z"},{"name":"sam","id":"LBJP07Z"}]}
我希望得到这样的结果:
tom
bob
bill
jill
tim
sam
我的代码:
for (int i = 0; i < [a count]-1; i++) {
for (int j = 0; j < [a[i] count]-1; j++)
{
NSString *str = [NSString stringWithFormat:@"%@",[[[[a objectAtIndex:i]objectForKey:@"data"]objectAtIndex:j] objectForKey:@"name"]];
NSLog(@"%@",str);
}
}
但最后,我得到了这样的结果:
tom
tom
tom
tom
tom
tom
答案 0 :(得分:1)
从你的JSON,a
是一个字典,而不是一个数组。获取data
数组开始:
NSArray *dataArray = a[@"data"];
现在,使用KVC提取名称:
NSArray *names = [dataArray valueForKey:@"name"];
答案 1 :(得分:0)
NSMutableDictionary *yourdict = [a JSONValue];
NSMutableArray *my_arr = [get_news objectForKey:@"data"];
[my_arr retain];