1

首先,我有这个获取 gdata 提要的 gdata url:

https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc

这是从 url 获取信息的代码:

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 成功获取缩略图。当我尝试访问NSLOGhdThumb网址时,我只收到null回复,所以我的问题是如何解决这个问题。

4

1 回答 1

1

尝试这个:

NSDictionary *thumbs = [[dataDictionary objectForKey:@"data"] objectForKey:@"thumbnail"];

或者您可以使用此语法来访问字典值:

NSDictionary *thumbs = dataDictionary[@"data"][@"thumbnail"];

你的问题是你忘了先访问“数据”字典

于 2013-10-06T20:28:32.397 回答