我從來沒有在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);
您對「NSError * error」的使用是完全錯誤的,但這不是主要問題。 – gnasher729 2014-11-25 13:45:44
你說你得到有效的JSON數據。但顯然你沒有。告訴我們你得到了什麼。 3840或3810 - 這是哪一個? – gnasher729 2014-11-25 13:46:57
3840 ..這是一個錯字。我編輯了我的帖子。 – KFP 2014-11-25 13:55:14