Кодируемая ошибка - ожидается декодирование словаряно нашел вместо этого массив - PullRequest
0 голосов
/ 21 мая 2018

Я пытаюсь декодировать JSON с помощью codable - https://pastebin.com/Xfjj2XiP

Однако при этом я получаю эту ошибку.

typeMismatch (Swift.толковый словарь, Swift.DecodingError.Context (codingPath: [CodingKeys (stringValue: "txt_forecast", intValue: nil)], debugDescription: "Ожидается декодирование словаряно вместо этого нашел массив. ", underError: 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)
    }
}

1 Ответ

0 голосов
/ 21 мая 2018

Имена переменных в 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)        
    }

}

Теперь должна быть возможность декодировать JSON из примера pastebin.

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