subodhbahl
subodhbahl

Reputation: 415

iPhone - NSDictionary - NSMutableArray

I am grabbing a JSON output into a NSDictionary, the JSON output looks somewhat like this -

{"1":[{"abc":11},{"abc":13}]}

I was successful in grabbing they value for the first key 1 using the following:

NSDictionary *getData = [JSONOutput objectForKey:@"1"];

Problem- Now, I want to grab they values for keys "abc" and throw it into a MutableArray. I am using this for grabbing the values for key abc but it doesn't seem to be working

int count = 0;
NSMutableArray *array = [NSMutableArray alloc] init];

for (NSString *key in getdata)
{
 [array addObject:[getdata objectForKey:@"abc"] atIndex:count];
count ++;
}

Note: JSONOutput is another dictionary where the JSON output is going in. I am allocating and initializing the dictionaries too.

Please help! I know its a really simple one but I have no clue where I am going wrong at..

Upvotes: 0

Views: 283

Answers (1)

Evan Mulawski
Evan Mulawski

Reputation: 55334

The element for key "1" is an array, not a dictionary (note the square bracket and the elements separated by a comma). The elements in the array are dictionaries. So:

NSArray *getData = [JSONOutput objectForKey:@"1"];
for (NSDictionary *dict in getdata)
{
    [array addObject:[dict objectForKey:@"abc"]];
}

Also, you don't need to use the count argument for addObject: if you are adding objects sequentially from zero.

Upvotes: 3

Related Questions