0

我一直在尝试查找某个用户的上传总数,并找到了以下线程。

如何检索特定 Youtube 用户上传的视频数量?

当我从浏览器点击 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);
4

1 回答 1

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"];
于 2013-10-08T17:49:40.843 回答