【问题标题】:Error in decoding: The data couldn't be read. Alamofire解码错误:无法读取数据。阿拉莫菲尔
【发布时间】:2018-12-03 22:21:45
【问题描述】:

我试图通过 JSONDecoder 解析 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
 }

【问题讨论】:

    标签: ios swift xcode alamofire decoder


    【解决方案1】:

    实际上响应结构与您在这一行尝试做的不同,

    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)
    
                }
    
              }
            }
     }
    

    您应该使用您在响应中获得的相同结构解码相应的对象。

    【讨论】:

    • 谢谢,但是print(weatherModel.city.name) 这不起作用,因为它表明weatherModel 没有成员city。还有其他想法吗?
    • 它会起作用,您需要将变量类型从数组更改为var weatherModel: WeatherModelDecodable
    • 现在它说Cannot assign value of type 'WeatherModelDecodable' to type 'WeatherModelDecodable.Type' 值必须是[WeatherModelDecodable]() 接下来要做什么?
    • 因为你还没有更新这个decode([WeatherModelDecodable].self。请参阅答案中的这一行。
    • @JoeHart 我包含了SecondTaskVC 供您参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多