I am trying to return videoId from the JSON file at a URL
{
"kind": "youtube#searchListResponse",
"etag": "imd66saJvwX2R_yqJNpiIg-yJF8",
"regionCode": "US",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "BeIXYopOmOl2niJ3NJzUbdlqSA4",
"id": {
"kind": "youtube#video",
"videoId": "1q7NUPF6j8c"
},
(Correct return in the example is 1q7NUPF6j8c). I would then like to load the video id in a
[self.playerView loadWithVideoId:@"the JSON video ID"];
So far I can return the entire JSON to the log with:
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:@"URL"]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSArray* json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
NSLog(@"YouTube ID: %@", json);
//NSArray *videoid = [json valueForKeyPath:@"items.0.id.videoId"];
//NSLog(@"VideoID: %@", videoid);
}] resume];
This returns to NSLOG:
**App [6094:1793786] YouTube ID: {
etag = "imd66saJvwX2R_yqJNpiIg-yJF8";
items = (
{
etag = BeIXYopOmOl2niJ3NJzUbdlqSA4;
id = {
kind = "youtube#video";
videoId = 1q7NUPF6j8c;
};
kind = "youtube#searchResult";**
(and the rest of the json).
I have tried this as a shot in the dark.
//NSArray *videoid = [json valueForKeyPath:@"items.0.id.videoId"];
//NSLog(@"VideoID: %@", videoid);
But that returns null.
How can I return just the videoID and pass it to the self.playerView loadWithVideoId?
NSLog(@"YouTube ID: %@", json);. BTW - It looks like thejsonvariable should beNSDictionary, notNSArray.videoIDthat you want. The point is to confirm that the actual structure you are getting matches what you are expecting.nilbecause the root object is a dictionary, note the{at the beginning.