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
Mikaila Akeredolu
10,654 PointsModifying the BlogReader iOS APP for other JSON data
Amit, After watching the blog reader app. I decided to create an app using the sam code but revising it for my project. My goal is to pull this JSON data using the blog reader code with slight changes to the properties . here is the link to the json data. http://opendata.socrata.com/resource/hecw-qndc.json. I even renamed the json data post and also tried using empty quotes because the json data had no name. The erorr message states: 2013-11-19 16:12:35.010 JSONdataAPP[3808:70b] -[NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8aeed30
2013-11-19 16:12:35.015 JSONdataAPP[3808:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8aeed30'
*** First throw call stack:
//////////////////////////////////////////////////////// I think the error is coming from the code below because of the above erorr message . what do you think?
NSURL *blogURL = [NSURL URLWithString:@"http://opendata.socrata.com/resource/hecw-qndc.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary =
[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.blogPosts = [NSMutableArray array];
NSArray *blogPostArray = [dataDictionary objectForKey:@"posts"];
//loop through array of dictionaries with data from dictionary and store into mutable array
for (NSDictionary *bpDictionary in blogPostArray) {
BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
blogPost.city = [bpDictionary objectForKey:@"city"];
//blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
//blogPost.date = [bpDictionary objectForKey:@"date"];
blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];
[self.blogPosts addObject:blogPost];
}
}
3 Answers
Amit Bijlani
Treehouse Guest TeacherWhen retrieving data from the web your ability to parse and display that data is completely dependent on its format. The format for your data is something like this:
[
{
community_colleges_universities: "Frederick Community College",
website: {
url: "http://www.frederick.edu/"
},
state: "MD"
},
{
community_colleges_universities: "Front Range Community College",
website: {
url: "http://www.frcc.cc.co.us/"
},
state: "CO"
}]
Instead of a dictionary that contains an array like we have for our the Treehouse blog, you have an array of items. You have to keep this in mind when parsing the JSON.
I would modify your code above to something like this:
// After parsing the JSON you get back an array of dictionary objects
NSArray *blogPostArray =
[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.blogPosts = [NSMutableArray array];
//loop through array of dictionaries with data from dictionary and store into mutable array
for (NSDictionary *bpDictionary in blogPostArray) {
BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"community_colleges_universities"]];
// I didn't see a city key in the JSON you linked
blogPost.city = [bpDictionary objectForKey:@"city"];
// If you simply ask for url you will get an error because it is embedded inside the "website" object.
// You can do a nested call where you access the url within the website by using the key "website.url"
blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"website.url"]];
[self.blogPosts addObject:blogPost];
}
Mikaila Akeredolu
10,654 Points@amit, You rock. this works but when I attempt to open the url in the detail web view I get nothing. Here is my code for transitioning to the web view to open the urls.
[segue.destinationViewController setBlogPostURL:blogPost.url];
// i tried this but it did not work: [segue.destinationViewController setBlogPostURL:blogPost.url.url];
Thanks for your time. truly appreciate the help.
Amit Bijlani
Treehouse Guest TeacherWhen parsing the URL in the for loop I forgot to mention that you need to use the method valueForKeyPath instead of objectForKey.
blogPost.url = [NSURL URLWithString:[bpDictionary valueForKeyPath:@"website.url"]];
Mikaila Akeredolu
10,654 PointsThanks Amit.