1

Long story, short. I need to access just the parent in the JSON file. How to parse multiple json in objective-c? ) I need to access the author > NAME from this JSON. (*removed link)

The code is:

NSURL *blogURL = [NSURL URLWithString:@"*removed link"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
//    NSLog(@"%@",dataDictionary);

    self.blogPosts = [NSMutableArray array];

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

    for (NSDictionary *bpDictionary in blogPostsArray) {
        BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
        blogPost.author = [bpDictionary objectForKey:@"author"];
        blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
        blogPost.date = [bpDictionary objectForKey:@"date"];
        blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];
        [self.blogPosts addObject:blogPost];
    }

How can i make it access that value ?

1 Answer 1

1

You should be able to use the dot notation

JSON

{
    "author": {
                "name" : "mckeejm"
              }
}

Objective C:

blogPost.author = [bpDictionary valueForKeyPath:@"author.name"];

updated Thanks @Martin

Sign up to request clarification or add additional context in comments.

2 Comments

valueForKey: is a KVC method, the correct way to access an object stored in a dictionary is via objectForKey:
You probably meant valueForKeyPath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.