简体   繁体   中英

How to get array from another array

I have a json array,like this:

 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. Get the data array to start:

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

Now, use KVC to extract the names:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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