0

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
1
  • 1
    Go to json.org and study the JSON syntax. It only takes 5-10 minutes to learn. Then you will be better able to understand what you're doing. Commented Feb 27, 2014 at 12:40

2 Answers 2

1

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"];
Sign up to request clarification or add additional context in comments.

Comments

0
NSMutableDictionary *yourdict = [a JSONValue];
NSMutableArray *my_arr = [get_news objectForKey:@"data"];
[my_arr retain];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.