2014-11-25 54 views
0

我從來沒有在web服務中使用過API,我在解析從Flickr API接收到的JSON數據時遇到了問題。我所知道的唯一事情(從我讀過的所有東西)都知道它很簡單,很簡單。關於我所能得到的就是在控制檯中返回一個字符串。使用字典返回null和或錯誤。我錯過了什麼?我希望能夠取出ID和所有者,以便我可以獲取照片網址。試圖從Flickr解析JSON數據。可可錯誤3840

這將返回我的照片數據:

NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSLog(@」%@」, json); //this returns data on my photo 

返回null(resultDict)和錯誤3840:

NSString *requestString = @」https://api.flickr.com/services/rest?&method=......etc; 
NSURL *url = [NSURL URLWithString:requestString]; 
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; 

NSURLSessionDataTask *task = [session dataTaskWithURL:url 
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    NSMutableDictionary *resultdict = [NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingMutableContainers error:&error]; 
    NSLog(@」%@」, resultDict); //returns null 
    If (error != nil) { NSLog(@」%@」, [error localizedDescription]); } 
    else { self.myDict = [[resultDict objectforKey:@」photos」] objectAtIndex:0]; 
    NSLog(@」%@」, self.myDict); } 
}]; 
[task resume]; 

要檢查,如果我有字典的數組我做了以下這返回0:

NSMutableArray *resultArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainer error:&error];                                                       error:&error]; 

NSLog(@"%lu", (unsigned long)resultArray.count); 
+0

您對「NSError * error」的使用是完全錯誤的,但這不是主要問題。 – gnasher729 2014-11-25 13:45:44

+0

你說你得到有效的JSON數據。但顯然你沒有。告訴我們你得到了什麼。 3840或3810 - 這是哪一個? – gnasher729 2014-11-25 13:46:57

+0

3840 ..這是一個錯字。我編輯了我的帖子。 – KFP 2014-11-25 13:55:14

回答

0

你肯定

[NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingMutableContainers error:&error]; 

返回一個字典,而不是Dictionnaries的數組?

編輯:

你可以嘗試用這個來請求API嗎? 我檢查了我的項目,我的迴應似乎與你的語法相同。 只有我使用的代碼是不同的。 (如果你可以給我們您所撥打的完整URL,這將是對我們比較容易^^「)

NSString *[email protected]"YOUR URL"; 
NSURL *url=[NSURL URLWithString:str]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; 
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error: &error]; 
NSMutableDictionary* resultList = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil]; 

我把它抄了沒有錯誤。我讓你管理它^^

+0

看起來像我認爲的2個字典的數組?我的json字符串返回:2014-11-25 08:54:24.607 Flickr_API [6915:553900] jsonFlickrApi({「photos」:{「page」:1,「pages」:1,「perpage」:10,「total」 :「1」,「photo」:[{「id」:「我的id#」,「所有者」:「我的主人#」,「祕密」:「祕密#」,「服務器」:「5604」 「:6中,」 標題 「:」 img_43" , 「ispublic」:1, 「isfriend」:0 「isfamily」:0}]}, 「STAT」: 「OK」}) 2014年11月25日08: 54:24.609 Flickr_API [6915:553900](空) 2014年11月25日08:54:24.616 Flickr_API [6915:553900]操作不能完成。 (可可錯誤3840) – KFP 2014-11-25 14:20:50

+0

看起來像帶2個密鑰的dictionnary:「照片」和「STAT」 數據[@「照片」]是具有一些密鑰的NSDictionnary,和「照片」項包含另一個NSDictionnary。 但我不明白你爲什麼不能序列化它 – Atomnium 2014-11-25 14:38:11

0
NSMutableDictionary ***resultdict** = [NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingMutableContainers error:&error]; 
NSLog(@」%@」, **resultDict**); //returns null 

resultdict和resultDict不是同一個變量。我敢打賭,你有一個實例變量或一個名爲resultDict的全局變量。實例變量應該以下劃線開頭,這是因爲它避免了這樣的問題。

0

對於問題製造者這個答案不會是實際的,但也許他們將與誰這樣的問題面對。 我試圖從flickr.photos.getRecent方法獲取數據時遇到同樣的問題。我忘了添加到URL參數值nojsoncallback=1

沒有它,你會得到JSONP響應。