长话短说。我只需要访问JSON文件中的父级。 How to parse multiple json in objective-c?) 我需要访问作者>来自此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];
}
如何让它访问该值?
答案 0 :(得分:1)
您应该可以使用点符号
JSON
{
"author": {
"name" : "mckeejm"
}
}
目标C:
blogPost.author = [bpDictionary valueForKeyPath:@"author.name"];
已更新感谢@Martin