如何从youtube API调用的响应中提取title
。
以下是获取YouTube视频详情的代码。
- (void)getVideoTitle{
// Set up your URL
NSString *youtubeApi = @"https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2C+snippet%2C+statistics&id=zB4I68XVPzQ&key=API_KEY";
NSURL *url = [[NSURL alloc] initWithString:youtubeApi];
// Create your request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// Send the request asynchronously
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) {
// Callback, parse the data and check for errors
if (data && !connectionError) {
NSError *jsonError;
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (!jsonError) {
NSLog(@"Response from YouTube: %@", jsonResult);
}
}
}] resume];
}
@end
这是回复。
Response from YouTube: {
etag = "\"m2yskBQFythfE4irbTIeOgYYfBU/3w7VzHWX6Z741beZqAzUx-AoGQQ\"";
items = (
{
contentDetails = {
caption = false;
definition = hd;
dimension = 2d;
duration = PT2M13S;
licensedContent = 1;
projection = rectangular;
};
etag = "\"m2yskBQFythfE4irbTIeOgYYfBU/riulsg6pv-HCB96fEN7edGYXYGg\"";
id = zB4I68XVPzQ;
kind = "youtube#video";
snippet = {
categoryId = 24;
channelId = "UCZGYJFUizSax-yElQaFDp5Q";
channelTitle = "Star Wars";
defaultAudioLanguage = en;
description = "Star Wars: The Last Jedi. Arriving in your galaxy December 15.\n\nVisit Star Wars at http://www.starwars.com\nSubscribe to Star Wars on YouTube at http://www.youtube.com/starwars\nLike Star Wars on Facebook at http://www.facebook.com/starwars\nFollow Star Wars on Twitter at http://www.twitter.com/starwars\nFollow Star Wars on Instagram at http://www.instagram.com/starwars\nFollow Star Wars on Tumblr at http://starwars.tumblr.com/";
liveBroadcastContent = none;
localized = {
description = "Star Wars: The Last Jedi. Arriving in your galaxy December 15.\n\nVisit Star Wars at http://www.starwars.com\nSubscribe to Star Wars on YouTube at http://www.youtube.com/starwars\nLike Star Wars on Facebook at http://www.facebook.com/starwars\nFollow Star Wars on Twitter at http://www.twitter.com/starwars\nFollow Star Wars on Instagram at http://www.instagram.com/starwars\nFollow Star Wars on Tumblr at http://starwars.tumblr.com/";
title = "Star Wars: The Last Jedi Official Teaser";
};
publishedAt = "2017-04-14T15:56:13.000Z";
tags = (
"star wars",
"the last jedi",
"daisy ridley",
"mark hamill",
"adam driver",
rey,
"kylo ren",
"luke skywalker",
lucasfilm,
trailer
);
thumbnails = {
default = {
height = 90;
url = "https://i.ytimg.com/vi/zB4I68XVPzQ/default.jpg";
width = 120;
};
high = {
height = 360;
url = "https://i.ytimg.com/vi/zB4I68XVPzQ/hqdefault.jpg";
width = 480;
};
maxres = {
height = 720;
url = "https://i.ytimg.com/vi/zB4I68XVPzQ/maxresdefault.jpg";
width = 1280;
};
medium = {
height = 180;
url = "https://i.ytimg.com/vi/zB4I68XVPzQ/mqdefault.jpg";
width = 320;
};
standard = {
height = 480;
url = "https://i.ytimg.com/vi/zB4I68XVPzQ/sddefault.jpg";
width = 640;
};
};
title = "Star Wars: The Last Jedi Official Teaser";
};
statistics = {
commentCount = 54170;
dislikeCount = 10550;
favoriteCount = 0;
likeCount = 341919;
viewCount = 14611544;
};
}
);
kind = "youtube#videoListResponse";
pageInfo = {
resultsPerPage = 1;
totalResults = 1;
};
}
答案 0 :(得分:0)
不确定这在目标C中如何起作用,但在swift中它将如下
if let dictionary = jsonResult as? NSDictionary {
if let itemArray = dictionary["items"] as? NSArray {
if let item = itemArray[0] as? NSDictionary {
if let snippet = item["snippet"] as? NSDictionary {
if let title = snippet["title"] as? String {
print(title)
}
}
}
}
}