解码错误:无法读取数据。阿拉莫菲尔
Error in decoding: The data couldn't be read. Alamofire
我试图通过 JSON 解码器解析 JSON 并使用 Alamofire 获取数据。但是,当我 运行 应用程序时,它显示由于格式不正确而无法读取数据。我尝试了很多东西,但仍然没有用。任何帮助,将不胜感激。来源如下:
VC:
class SecondTaskVC: UIViewController {
var weatherModel = [WeatherModelDecodable]()
override func viewDidLoad() {
let url = URL(string: "https://api.openweathermap.org/data/2.5/forecast?lat=42.874722&lon=74.612222&APPID=079587841f01c6b277a82c1c7788a6c3")
Alamofire.request(url!).responseJSON { (response) in
let result = response.data
do{
let decoder = JSONDecoder()
self.weatherModel = try decoder.decode([WeatherModelDecodable].self, from: result!) // it shows this line as a problem
for weather in self.weatherModel {
print(weather.city.name)
}
}catch let error{
print("error in decoding",error.localizedDescription)
}
}
}
}
数据模型:
struct WeatherModelDecodable: Decodable {
let city: CityDecodable
}
struct CityDecodable: Decodable {
let name: String
}
实际上响应结构与您在这一行尝试做的不同,
self.weatherModel = try decoder.decode([WeatherModelDecodable].self, from: result!)
响应不是数组,您可以在任何浏览器中点击此 Url 在 json viewer 中看到它。您期待一个 json 对象数组,但它不是。所以如果你将它解码为单个对象,它将正确解码如下,
let weatherModel = try decoder.decode(WeatherModelDecodable.self, from: result!)
print(weatherModel.city.name)
所以,SecondTaskVC
看起来像这样,
class SecondTaskVC: UIViewController {
var weatherModel: WeatherModelDecodable?
override func viewDidLoad() {
let url = URL(string: "https://api.openweathermap.org/data/2.5/forecast?lat=42.874722&lon=74.612222&APPID=079587841f01c6b277a82c1c7788a6c3")
Alamofire.request(url!).responseJSON { (response) in
let result = response.data
do{
let decoder = JSONDecoder()
self.weatherModel = try decoder.decode(WeatherModelDecodable.self, from: result!)
print(self.weatherModel!.city.name)
}catch let error{
print("error in decoding",error.localizedDescription)
}
}
}
}
您应该使用您在响应中获得的相同结构来解码相应的对象。
我试图通过 JSON 解码器解析 JSON 并使用 Alamofire 获取数据。但是,当我 运行 应用程序时,它显示由于格式不正确而无法读取数据。我尝试了很多东西,但仍然没有用。任何帮助,将不胜感激。来源如下:
VC:
class SecondTaskVC: UIViewController {
var weatherModel = [WeatherModelDecodable]()
override func viewDidLoad() {
let url = URL(string: "https://api.openweathermap.org/data/2.5/forecast?lat=42.874722&lon=74.612222&APPID=079587841f01c6b277a82c1c7788a6c3")
Alamofire.request(url!).responseJSON { (response) in
let result = response.data
do{
let decoder = JSONDecoder()
self.weatherModel = try decoder.decode([WeatherModelDecodable].self, from: result!) // it shows this line as a problem
for weather in self.weatherModel {
print(weather.city.name)
}
}catch let error{
print("error in decoding",error.localizedDescription)
}
}
}
}
数据模型:
struct WeatherModelDecodable: Decodable {
let city: CityDecodable
}
struct CityDecodable: Decodable {
let name: String
}
实际上响应结构与您在这一行尝试做的不同,
self.weatherModel = try decoder.decode([WeatherModelDecodable].self, from: result!)
响应不是数组,您可以在任何浏览器中点击此 Url 在 json viewer 中看到它。您期待一个 json 对象数组,但它不是。所以如果你将它解码为单个对象,它将正确解码如下,
let weatherModel = try decoder.decode(WeatherModelDecodable.self, from: result!)
print(weatherModel.city.name)
所以,SecondTaskVC
看起来像这样,
class SecondTaskVC: UIViewController {
var weatherModel: WeatherModelDecodable?
override func viewDidLoad() {
let url = URL(string: "https://api.openweathermap.org/data/2.5/forecast?lat=42.874722&lon=74.612222&APPID=079587841f01c6b277a82c1c7788a6c3")
Alamofire.request(url!).responseJSON { (response) in
let result = response.data
do{
let decoder = JSONDecoder()
self.weatherModel = try decoder.decode(WeatherModelDecodable.self, from: result!)
print(self.weatherModel!.city.name)
}catch let error{
print("error in decoding",error.localizedDescription)
}
}
}
}
您应该使用您在响应中获得的相同结构来解码相应的对象。