davelowe85
davelowe85

Reputation: 1109

Parsing JSON for Blog App in xcode using Objective C

I am trying to link to a thumbnail image stored on a server using JSON. The code I have so far is:

NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"];

for (NSDictionary *bpDictionary in blogPostsArray) {
    BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];

    blogPost.thumbnail = ???

    //blogPost.date = [bpDictionary objectForKey:@"date"];
    [self.blogPosts addObject:blogPost];
}

As you can see where the 3 question marks are is where I am stuck. Here is the JSON file I am working from, I need to know how to get down to the thumbnail url and assign it to blogPost.thumbnail:

enter image description here

Upvotes: 0

Views: 83

Answers (2)

Havic
Havic

Reputation: 117

blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];

Upvotes: 0

Wain
Wain

Reputation: 119041

Try using key path navigation:

NSArray *attachments = [bpDictionary objectForKey:@"attachments"];

if (attachments.count > 0) {
    blogPost.thumbnail = [attachments[0] valueForKeyPath:@"images.thumbnail.url"];
}

Upvotes: 1

Related Questions