Json 带有URL的URLSession返回错误
我读过关于SO的主题,但还没有找到答案。 我一直在尝试使用天气api为我的应用程序下载天气数据。奇怪的是,我可以在没有“?”的url上运行它,但这个url内置了“?”。我怀疑这就是问题所在,但我该如何解决它,或者让它忽略它?无论如何,这是我的理论。代码如下:Json 带有URL的URLSession返回错误,json,swift,weather,urlsession,Json,Swift,Weather,Urlsession,我读过关于SO的主题,但还没有找到答案。 我一直在尝试使用天气api为我的应用程序下载天气数据。奇怪的是,我可以在没有“?”的url上运行它,但这个url内置了“?”。我怀疑这就是问题所在,但我该如何解决它,或者让它忽略它?无论如何,这是我的理论。代码如下: struct WeatherData: Decodable { let description: String let temp: Double let wind: Double } func weather()
struct WeatherData: Decodable {
let description: String
let temp: Double
let wind: Double
}
func weather() {
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID={40b5f59a0004885043fe3df3e0b6ed8e}"
let urlObj = URL(string: url)
URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
do {
let weatherObj = try JSONDecoder().decode([WeatherData].self, from: data!)
print(data!)
for weather in weatherObj {
print(weather.temp, weather.description, weather.wind)
}
} catch {
print("Got an Error")
}
}.resume()
}
因此,按原样运行会产生错误:“线程1:致命错误:在展开可选值时意外发现nil”或URLSession行
我是否遗漏了一些非常明显的东西,或者有没有办法解决这个问题
非常感谢
--
更新:
因此,在更改结构和{}之后,它一直在工作,直到我开始将数据输入到标签中。以下是最新的尝试:
func weather() {
let lat = locationManager.location!.coordinate.latitude
let long = locationManager.location!.coordinate.longitude
//let baseURL = "http://api.openweathermap.org/data/2.5/weather?"
let apiKey = "40b5f59a0004885043fe3df3e0b6ed8e"
//let weatherURL = URL(string: "\(baseURL)lat=\(lat)&lon=\(long)&units=metric&APPID=\(apiKey)")
let weahterURL = "http://api.openweathermap.org/data/2.5/weather?lat=\(lat)&lon=\(long)&units=metric&APPID=\(apiKey)"
//let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID=40b5f59a0004885043fe3df3e0b6ed8e"
let urlObj = URL(string: weahterURL)
URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
do {
let weatherObj = try JSONDecoder().decode(WeatherData.self, from: data!)
print(weatherObj)
//seems as though not gettign any data from beyond this point
var desc = weatherObj.weather
var wind = weatherObj.wind.speed
var tempMin = weatherObj.main.temp_min
var tempMax = weatherObj.main.temp_max
DispatchQueue.main.async {
self.weatherDesc = desc
self.weartherWind.text = wind
self.tempMinTxt.text = tempMin
self.tempMaxTxt.text = tempMax
}
} catch {
print("Got an Error", error.localizedDescription)
}
}.resume()
}
您错误地构造了url而不是
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID={40b5f59a0004885043fe3df3e0b6ed8e}"
做
这个
应该是
40b5f59a0004885043fe3df3e0b6ed8e
另外为解码器创建的结构无效,无法获取数据
//
//
//
//
//
您错误地构造了url而不是
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,GB?&units=imperial&APPID={40b5f59a0004885043fe3df3e0b6ed8e}"
做
这个
应该是
40b5f59a0004885043fe3df3e0b6ed8e
另外为解码器创建的结构无效,无法获取数据
//
//
//
//
//
移除{}。移除{}。这就是我猜想的新问题。我遗漏了什么?我刚刚尝试使用您发布的新结构,但现在我得到了错误“无法读取数据,因为它的格式不正确。”我讨厌JSON:(查看答案中的最后一行,而不是[WeatherData]。self do WeatherData.self也只打印(weatherObj)然后删除for循环,所以我删除了for循环,只打印了weatherObj。起初,我在输出中看到返回的数据,然后,当我开始尝试将其放入vars以添加到标签中时,我再次遇到错误。“数据无法读取,因为它的格式不正确”所以我拿走了VAR,错误依然存在。这就是我猜测的新问题。我遗漏了什么?我刚刚用你发布的新结构尝试了它,但现在我得到了错误“数据无法读取,因为它的格式不正确。”我讨厌JSON:(看回答中的最后一行,而不是[WeatherData].self do WeatherData.self也只打印(weatherObj)并删除for循环,因此我删除了for循环并只打印了weatherObj。起初,我在输出中看到返回的数据,然后,当我开始尝试将其放入vars以添加到标签时,我再次遇到错误。“无法读取数据,因为它的格式不正确”所以我拿走了VAR,错误依然存在。
struct WeatherData: Decodable {
let weather: [WeatherItem]
let wind: WindItem
let main : MainItem
}
struct WeatherItem: Decodable {
let description: String
}
struct WindItem: Decodable {
let speed: Double
let deg: Double
}
struct MainItem : Decodable {
let tempMin: Double
let tempMax: Double
private enum CodingKeys: String, CodingKey {
case tempMin = "temp_min" , tempMax = "temp_max"
}
}
let weatherObj = try JSONDecoder().decode(WeatherData.self, from: data!)