Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nikki R
Courses Plus Student 100 PointsWhat if JSON is an array of dictionaries
So I followed along in the Blog Reader tutorial but the api I want to mess around with has its JSON format as an array of dictionaries. I can print out the array of dictionaries in the code but when I try to iterate through it, it's null, there's some added step I'm missing. Please help. Thanks.
from my api: [ { id: 60, name: "blah" }, { id: 61, name: "yadda yadda yadda" } ]
NSURL *blogURL = [NSURL URLWithString:@"myAPIURL"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
--I have tried making dataDictionary an NSArray, a NSMutableArray, no luck.
Nikki R
Courses Plus Student 100 PointsThank you for the reply, if I log jsonData, I get:
<5b7b2269 64223a36 30362c22 6e616d65 223a2243 61726579 20486172 74205472 69706c65 20546872 65617420 2d205375 72662c20 4d6f746f 2c20536e 6f77222c 22637265 61746564 5f617422 3a223230 31332d30 362d3133 2030393a 30363a34 30205554 43222c22 75706461 7465645f 6174223a 22323031 332d3036 2d313320 30393a30 363a3430 20555443 222c2275 6e697175 655f6964 223a2271 3058484d 386c5f4e 4559222c 2275706c 6f616465 645f6174 223a2232 3031312d 30332d32 32203232 3a34353a 32332e30 30303030 30222c22 7468756d 626e6169 6c5f7569 64223a22 30325f...2031323a 31343a35 37205554 43227d5d>
Then logging dataDictionary:
BlogReader[17882:c07] (
{
id = 60;
name = "blah";
},
{
id = 61;
name = "yadda yadda yadda";
}
)
it's this line in the original blog reader tutorial where the app crashes with my api:
self.blogPosts = [dataDictionary objectForKey:@"tags"];
blogPosts is an NSArray and because the json file starts with an array containing dictionaries, there's no key:value pair and nothing to replace tags with.
1 Answer
Ben Jakuben
Treehouse TeacherThis is weird - your JSON response has parenthesis where it should have curly braces. Somebody else posted a similar issue recently. Can you reply with the URL you are using to request the data? The data that your log shows is technically invalid JSON, which is probably why your code isn't working.
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherCan you paste in your full JSON response?
Then, let's break down your code line by line to make sure it's working each step along the way. What does it look like if you log
jsonData? Is that variable getting set correctly? You'll want to make certain that is correct before trying to use it to setdataDictionary.