我在swift 4中尝试新的JSONDecoder()并尝试解析以下flickr API响应:(https://api.flickr.com/services/rest/?api_key=api_key&method=flickr.photos.search&format=json&per_page = 25&text = hello&nojsoncallback = 1).但是我得到一个错误,说"处理json数据时出错:由于数据格式不正确,无法读取数据." 但是数据似乎对我来说格式正确.我做错了吗?
let dataTask = session.dataTask(with: urlRequest) { (data, response, error) in
if let error = error {
print("json error: \(error.localizedDescription)")
return
} else if let data = data {
print(response)
do {
let decoder = JSONDecoder()
print(data)
let flickrPhotos = try decoder.decode(FlickrImageResult.self, from: data)
} catch {
dump(data)
print("json error: \(error.localizedDescription)")
}
}
}
dataTask.resume()
Run Code Online (Sandbox Code Playgroud)
我的数据模型是
struct FlickrImageResult : Codable {
var photos : FlickrPhoto? = nil
}
struct …Run Code Online (Sandbox Code Playgroud)