【发布时间】:2019-09-16 22:03:36
【问题描述】:
目前正在通过一个从 OpenWeatherMap API 获取和解码数据的应用程序工作,目前除了让解码器返回某些内容外,我已经完成了所有工作。目前,解码器返回 nil,但是,我从 API 调用中获取数据字节。我不确定可能是什么问题。我已经根据层次结构设置了 ViewModel 结构。 OPW API JSON 数据似乎是字典键:值对集合类型的格式,键用引号括起来,是不是我的解码器因为引号而没有找到必要的信息?
获取和解码 API 调用...
@IBAction func saveCityButtonPressed() {
if let city = cityNameTextField.text {
let weatherURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(city)&APPID=8bad8461edbaf3ff50aa2f2fd8ad8a71&units=imperial")!
let weatherResource = Resource<WeatherViewModel>(url: weatherURL) { data in
let weatherVM = try? JSONDecoder().decode(WeatherViewModel.self, from: data)
return weatherVM
}
Webservice().load(resource: weatherResource) { result in
}
}
}
视图模型
struct WeatherListViewModel {
private var weatherViewModels = [WeatherViewModel]()
}
struct WeatherViewModel: Decodable {
let name: String
let main: TemperatureViewModel
}
struct TemperatureViewModel: Decodable {
let temp: Double
let temp_min: Double
let temp_max: Double
}
JSON 数据示例:
{
"coord":{
"lon":-0.13,
"lat":51.51
},
"weather":[
{
"id":300,
"main":"Drizzle",
"description":"light intensity drizzle","icon":"09d"
}
],
"base":"stations",
"main":{
"temp":280.32,
"pressure":1012,
"humidity":81,
"temp_min":279.15,
"temp_max":281.15
},
"visibility":10000,
"wind":{
"speed":4.1,
"deg":80
},
"clouds":{
"all":90
},
"dt":1485789600,
"sys":{
"type":1,
"id":5091,
"message":0.0103,
"country":"GB",
"sunrise":1485762037,
"sunset":1485794875
},
"id":2643743,
"name":"London",
"cod":200
}
【问题讨论】:
-
说
let weatherVM = try? JSONDecoder().decode(WeatherViewModel.self, from: data)是愚蠢的。相反,使用try,将它包装在一个do/catch 结构中,catch 错误,然后打印出来!然后您将立即看到问题所在,因为会显示一个巨大的详细错误消息。 -
不要
try?。 从不try?在使用Decodable解码 JSON 时。catch错误,它会告诉你到底出了什么问题。提示:根对象中没有city和main键 -
就在这里。这是你愚蠢地、故意地扔掉的有用信息!
Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "city", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"city\", intValue: nil) (\"city\").", underlyingError: nil)) -
感谢 matt 和 vadian,我现在就试试。我正在关注一个讲座项目,这是我遇到的一个问题,幸好我向 SO 寻求帮助。
-
抱歉:JSON 中有一个
main键。city与forecast相关(与weather不同)API