Access nested arrays and dictionaries generated by JSON-framework iPhone
-
20-09-2019 - |
Question
I'm using a Google API to return some JSON, which i have converted to their Objective C types using the JSON-framework (Stig B - Google Code).
I now have structures like this:
responseData
results
[0]
title = "Stack Overflow"
cursor
How can i access the nested array results
to get at the title
value (dictionary i'm guessing)?
I have tried this but no success:
for (NSString *key in [jsonObjects objectForKey:@"responseData"]) {
NSLog(@"%@",key);
for (NSString *element in [key valueForKey:@"results"]) {
NSLog(@"%@",element);
}
}
The outer loop will print out the names of the arrays results
and cursor
so that works, but for the inner loop, I get a not key value coding compliant
error.
Thanks
Solution
You can use NSLog([jsonObjects description])
to see the contents and structure of your dictionary.
To browse to your "results" array and its contents you can use the following (or similar) code:
NSDictionary* responseDict = [jsonObjects objectForKey:@"responseData"]; // Get your dictionary
NSArray* resultsArray = [responseDict objectForKey:@"results"];
for (NSDictionary* internalDict in resultsArray)
for (NSString *key in [internalDict allKeys])
NSLog([NSString stringWithFormat:@"%@ - %@", key, [internalDict objectForKey:key];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow