2013-10-08 73 views
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); 

回答

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"]; 
+0

這個作品!!!謝謝!我只是添加了一些類型轉換來獲得整數值。往上看! :D –

+0

很好聽,不確定你需要的數據類型。 upvote也將不勝感激 –

相關問題