swift - Codable 错误 - 预期解码 Dictionary<String, Any> 但发现了一个数组

标签 swift codable

我正在尝试借助 codable 解码 JSON - https://pastebin.com/Xfjj2XiP

但是,当我这样做时出现了这个错误。

typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "txt_forecast", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

这是我使用的代码:

struct container: Decodable {
    var days: [forecastDay]

    //Coding keys
    enum CodingKeys: String, CodingKey {
        case forecast = "forecast"
        case txt_forecast = "txt_forecast"
        case forecastday = "forecastday"
    }

    // Decoding
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let forecast = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .forecast)
        let txt_forecast = try forecast.nestedContainer(keyedBy: CodingKeys.self, forKey: .txt_forecast)
        let forecastdays = try txt_forecast.nestedContainer(keyedBy: CodingKeys.self, forKey: .forecastday)

        let forecastdaysData = try forecastdays.decode(String.self, forKey: .forecastday)

        days = try JSONDecoder().decode([forecastDay].self, from: forecastdaysData.data(using: .utf8)!)

        print(days)

    }
}

struct forecastDay: Decodable {
    var period: Int?
    var icon: String?
    var title: String?
    var fcttext: String?

    //Coding keys
    enum CodingKeys: String, CodingKey {
            case period = "period"
            case icon = "icon"
            case title = "title"
            case fcttext = "fcttext"
    }

    // Decoding
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        period = try container.decode(Int.self, forKey: .period)

        icon = try container.decode(String.self, forKey: .icon)

        title = try container.decode(String.self, forKey: .title)

        fcttext = try container.decode(String.self, forKey: .fcttext)
    }
}

最佳答案

forecastDay 中的变量名称与 JSON 中的键没有区别,init(from decoder:) 也没有任何自定义,因此预测日的结构期间可以简化。

struct ForecastDayPeriod: Decodable {
    let period: Int
    let icon: String
    let title: String
    let fcttext: String
}

现在最好为 JSON 中的每个级别使用带有键的枚举。此外,init(from decoder:) 不应创建新的 JSONDecoder

struct ForecastDay: Decodable {

    let periods: [ForecastDayPeriod]

    enum CodingKeys: String, CodingKey {
        case forecast
    }

    enum ForecastKeys: String, CodingKey {
        case txtForecast = "txt_forecast"
    }

    enum TxtForecastKeys: String, CodingKey {
        case forecastday
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        let forecast = try values.nestedContainer(keyedBy: ForecastKeys.self,
                                                  forKey: .forecast)
        let txtForecast = try forecast.nestedContainer(keyedBy: TxtForecastKeys.self,
                                                       forKey: .txtForecast)

        periods = try txtForecast.decode([ForecastDayPeriod].self,
                                         forKey: .forecastday)        
    }

}

现在应该可以从 pastebin 示例中解码 JSON。

do {
    let jsonData: Data = ...
    let forecastDay = try JSONDecoder().decode(ForecastDay.self, from: jsonData)
} catch {
    print("Error: \(error)")
}

关于swift - Codable 错误 - 预期解码 Dictionary<String, Any> 但发现了一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439824/

相关文章:

ios - `convertFromSnakeCase` 策略不适用于 Swift 中的自定义 `CodingKeys`

json - 在 Swift 中解码 Codable

Swift 4.1 - Set 扩展要求 Element 符合 Encodable

json - 尝试解析带引号的 JSON 响应时出现错误

ios - 如何在 3.5 英寸 Iphone 上仅允许 uiscrollview

swift - 将 Binding<Double> 类型的两个值转换为 Binding<CGPoint>

ios - 在 Swift (iOS) 中获取设备的蓝牙名称和连接的蓝牙

ios - 使用相机模式拍摄图像时出错

ios - Swift: TableView 父类(super class)错误

json - Swift JSON 从数组解析开始