How to browse NSDictionary?

I've written the following code which should browse an NSDictionary :

json_string=@"[{\"COMPAIN\":\"44\"},{\"COMPAIN\",\"46\"}]";
     NSArray *statuses = [parser objectWithString:json_string error:nil];
     for (NSDictionary *status in statuses)
     {                  
          NSLog(@"%@", [status objectForKey:@"COMPAIN"]);
     }  
            

However, this code isnt browsing correctly.

How can I fix the above code in order to browse an NSDictionary?


It's because your *status isn't a NSDictionary and your *statuses isn't an NSArray.

Your json-string is an associative array. However, it contains the same key 2 times.

Try replacing your JSON-string (so edit your back-end) so it becomes:

[{"COMPAIN":"44"}, {"COMPAIN", "46"}]

If you use PHP, just use json_encode and PHP will generate a json-string out of it =)!


// json_string now is [{"COMPAIN":"44"}, {"COMPAIN", "46"}]; so ...
NSArray *statuses = [parser objectWithString:json_string error:nil];
for (NSDictionary *status in statuses)
{                  
     NSLog(@"%@", [status objectForKey:@"COMPAIN"]);
}

This is the same code as you already was using, so it should work now =)!