简体   繁体   English

如何从另一个数组获取数组

[英]How to get array from another array

I have a json array,like this: 我有一个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"}]}

I want get results like this: 我想要这样的结果:

tom
bob
bill
jill
tim
sam

My code : 我的代码:

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);

    }
}

But in the end,I get results like this: 但是最后,我得到这样的结果:

tom
tom
tom
tom
tom
tom

From your JSON, a is a dictionary, not an array. 在您的JSON中, a是字典,而不是数组。 Get the data array to start: 获取data数组以开始:

NSArray *dataArray = a[@"data"];

Now, use KVC to extract the names: 现在,使用KVC提取名称:

NSArray *names = [dataArray valueForKey:@"name"];
NSMutableDictionary *yourdict = [a JSONValue];
NSMutableArray *my_arr = [get_news objectForKey:@"data"];
[my_arr retain];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM