1

长话短说。我只需要访问 JSON 文件中的父级。 如何在objective-c中解析多个json?) 我需要从此 JSON访问作者 > NAME 。(*删除链接)

代码是:

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];
    }

我怎样才能让它访问那个值?

4

1 回答 1

1

您应该能够使用点符号

JSON

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

目标 C:

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

更新感谢@Martin

于 2013-07-25T15:12:46.100 回答