У меня есть эти JSON
данные:
{
"cities": [
{
"id": 1,
"name": "Paris",
"country_code": "FR",
"attractions": [
"CityHall",
"Theatre",
"Port"
],
"population": 0
},
{
"id": 2,
"name": "Nice",
"country_code": "FR",
"attractions": [
"CityHall"
],
"population": 0
},
{
"id": 3,
"name": "Berlin",
"country_code": "DE",
"attractions": [
"Theatre",
"Port"
],
"population": 0
},
{
"id": 4,
"name": "Munich",
"country_code": "DE",
"attractions": [
"Theatre"
],
"population": 0
},
{
"id": 5,
"name": "Amsterdam",
"country_code": "NL",
"attractions": [
"Theatre",
"Port"
],
"population": 0
},
{
"id": 6,
"name": "Leiden",
"country_code": "NL",
"attractions": [
"CItyHall",
"Theatre"
],
"population": 0
}
]
}
Я расшифровываю его с помощью Moya .request(.getCities).map([City].self, atKeyPath: "cities")
, используя объект:
struct City {
let id: Int
let name: String
let countryCode: String
//let attractions: [Attraction]
let population: Int
}
extension City: Decodable {
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case countryCode = "countryCode"
//case attractions = "attractions"
case population = "population"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
countryCode = try container.decode(String.self, forKey: .countryCode)
//attractions = try container.decode([Attraction].self, forKey: .attractions)
population = try container.decode(Int.self, forKey: .population)
}
}
Просто и красиво, но проблема в том, что я не могу понять, как вписать сюда этот массив attractions
. У меня это как enum
и я пытался получить ключи с помощью codingPath
enum Attraction: String {
case CityHall
case Theatre
case Port
}
extension Attraction: Decodable {
enum CodingKeys: String, CodingKey {
// confusion mode initiated :) do I use it the same as in struct here?
case cityHall = "CityHall"
case theatre = "Theatre"
case port = "Port"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let attraction = container.codingPath.first!.stringValue // that would get the first key into string but how to deal with enum?
}
}
Кроме того, если это будет нормально декодировать attractions
, будет ли объект City
нормально декодировать вложенный объект/массив?
atractions
в вашем JSON написано с ошибкой. Вы можете что-нибудь с этим сделать? - person Sweeper   schedule 26.09.2020