0
我一直在嘗試查找某個用戶上傳的總數,並找到以下線索。通過JSON獲取來自YouTube用戶的上傳總數
How can I retrieve the number of videos uploaded by a specific Youtube user?
回答這個問題的偉大工程,都當我打的URL從瀏覽器,但是當我的OBJ-C去打下面的網址爲我返回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);
這個作品!!!謝謝!我只是添加了一些類型轉換來獲得整數值。往上看! :D –
很好聽,不確定你需要的數據類型。 upvote也將不勝感激 –