通过JSON获取YouTube用户的上传总数

时间:2013-10-08 17:11:16

标签: ios objective-c json youtube youtube-api

我一直试图找到某个用户上传的总数,并找到了以下帖子。

How can I retrieve the number of videos uploaded by a specific Youtube user?

这个问题的答案很有效,当我从浏览器点击URL时,但是当我进入Obj-C以点击以下URL时,它会为我返回NULL。

https://gdata.youtube.com/feeds/api/users/megangore/uploads?v=2&alt=jsonc&max-results=0

我正在使用以下代码,有人可以告诉我我做错了什么吗?我在这里疯了。

NSString *URL = @"https://gdata.youtube.com/feeds/api/users/megangore/uploads?v=2&alt=jsonc&max-results=2";
NSLog(@"*******%@", URL);

dispatch_async(kBgQueue, ^{NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
    @autoreleasepool{
        NSLog(@"******* PERFORMING STUFF *******");
        [self performSelectorOnMainThread:@selector(processOrders:)withObject:data waitUntilDone:YES];
    }
});

然后又调用

//GOES TO SERVER AND LOOKS AT WORK ORDERS
-(void)processOrders:(NSData *)responseData
{

NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];

//AMOUNT OF WORK ORDERS
NSArray* Orders = [json objectForKey:@"body"]; //2
NSDictionary* nextOrder = [Orders objectAtIndex:0];
NSString* TotalNumber = [nextOrder objectForKey:@"totalItems"];
NSLog(@"**********************************%@", TotalNumber);
}

此外:

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1

这是答案

NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];

//AMOUNT OF WORK ORDERS
NSDictionary *returnedData = json[@"data"];
NSNumber *uploadCount = returnedData[@"totalItems"];
int value = [uploadCount intValue];
NSLog(@"loans: %i", value);

1 个答案:

答案 0 :(得分:1)

查看提供的链接的输出

 {"apiVersion":"2.1","data":{"updated":"2013-10-08T17:44:29.337Z","totalItems":115,"startIndex":1,"itemsPerPage":0}}

没有“body”键,也没有数组。如果上传的总项数计算在内,

   NSDictionary *returnedData = json[@"data"];
   NSNumber *uploadCount = returnedData[@"totalItems"];