首先,我有这个gdata网址,它获取了一个gdata feed:
https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc
这是从网址获取信息的代码:
NSURL *feedURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc"];
NSData *jsonData = [NSData dataWithContentsOfURL:feedURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *data1 = [dataDictionary objectForKey:@"data"];
NSString *t = [data1 objectForKey:@"title"];
NSLog(@"Title:%@", t);
NSDictionary *thumbs = [dataDictionary objectForKey:@"thumbnail"];
NSURL *standardThumb = [thumbs objectForKey:@"sqDefault"];
NSURL *hdThumb = [thumbs objectForKey:@"hqDefault"];
上面的代码获取视频的标题。但是,代码无法从gdata URL成功获取缩略图。当我尝试NSLOG
hdThumb
网址时,我只会收到null
个回复,所以我的问题是如何解决这个问题。
答案 0 :(得分:1)
试试这个:
NSDictionary *thumbs = [[dataDictionary objectForKey:@"data"] objectForKey:@"thumbnail"];
或者您可以使用此语法来访问字典值:
NSDictionary *thumbs = dataDictionary[@"data"][@"thumbnail"];
你的问题是你忘了访问"数据"字典第一