2017-04-15 20 views
0

如何從youtube API調用的響應中提取titleios - 從YouTube提取標題API v3響應

下面是獲取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; 
    }; 
} 
+1

你嘗試過搜索解析JSON? –

回答

0

不知道如何做到這一點的目標C的工作,但在迅速的將是如下

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) 
       } 
      } 
     } 
    } 
} 
+0

不需要第一行,因爲jsonResult已經是NSDictionary了 – Spads