0

如何使用来自 youtube 的 gdata 获取单个视频信息。我知道我可以使用此代码获取播放列表:但是该代码无法获取单个视频的数据

NSURL *blogURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/playlists/PL85E8F3CC22BCEDC6?v=2&alt=jsonc"];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];


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

NSDictionary *item = [data1 objectForKey:@"items"];

for (NSDictionary *video in item) {
    NSDictionary *title = [video objectForKey:@"video"];
    NSArray *titleArray = [title objectForKey:@"title"];

    NSLog(@"%@",titleArray);

    NSString*t  = [title objectForKey:@"title"];
    NSLog(@"Title:%@", t);

    ...
}

但我不知道如何获取单个视频。任何帮助将不胜感激...谢谢...

4

1 回答 1

0

要使用 v2 获取单个视频的提要,您需要将第一行中的 URL 替换为如下内容:

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

根据需要更改 videoID 的位置。

与此提要一起返回的 JSON 自然会略有不同;因此,与其为项目列表创建一个 NSDictionary 对象然后循环遍历它,不如执行以下操作:

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 = [data1 objectForKey:@"thumbnail"]; 

NSURL *standardThumb  = [thumbs objectForKey:@"sqDefault"];
NSURL *hdThumb  = [thumbs objectForKey:@"hqDefault"];

编辑代码以反映评论中的问题

于 2013-10-03T03:53:53.473 回答