我一直在尝试查找某个用户的上传总数,并找到了以下线程。
如何检索特定 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);
查看提供的链接的输出
{"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"];