【问题标题】:Parsing different JSON feeds in Swift to the same Decodable struct将 Swift 中的不同 JSON 提要解析为相同的 Decodable 结构
【发布时间】:2020-02-01 09:40:54
【问题描述】:

我有两个不同的 JSON 提要,

{
 "uid":9018823,
 "lat":43.25394,
 "lng":-2.93844,
 "bike":false,
 "name":"02-STATION",
 "address":null,
 "spot":true,
 "number":3388,
 "bikes":3,
 "booked_bikes":0,
 "bike_racks":20,
 "free_racks":16
}

{
 "address":null,"last_updated":1580546431,
 "renting":1,"returning":1,"uid":"3348"},
 "free_bikes":17,
 "id":"0a0d9d6e93abd05548c672b60bfa9099",
 "latitude":40.677236,
 "longitude":-74.015665,
 "station_name":"Coffey St & Conover St",
 "timestamp":"2020-02-01T09:26:31.254000Z"
}

我想要解析填充以下结构的两个提要,


struct Places: Codable {

    var name: String
    let lat: Double
    let lng: Double
}

正如我所见,我可以在 init(from decoder:Decoder) 中使用 Decodable 来做到这一点,但我无法绕开它并让它发挥作用。

【问题讨论】:

  • 对于第二个 JSON,您希望name 具有什么值?

标签: json swift decodable


【解决方案1】:

解决这个问题的一种方法是为每个 json 消息类型设置一个 CodingKey 枚举,然后当尝试使用一个枚举创建容器时,我们会捕获任何错误而不是抛出它并尝试使用另一个枚举创建一个容器

struct Places: Decodable {
    let name: String
    let lat: Double
    let lng: Double

    enum CodingKeys1: String, CodingKey {
        case name
        case lat
        case lng
    }

    enum CodingKeys2: String, CodingKey {
        case name = "station_name"
        case lat = "latitude"
        case lng = "longitude"
    }

    init(from decoder: Decoder) throws {
        do {
            let container = try decoder.container(keyedBy: CodingKeys1.self)
            try self.init(container)
        } catch {
            let container = try decoder.container(keyedBy: CodingKeys2.self)
            try self.init(container)
        }
    }

    private init(_ container: KeyedDecodingContainer<CodingKeys1>) throws {
        name = try container.decode(String.self, forKey: .name)
        lat = try container.decode(Double.self, forKey: .lat)
        lng = try container.decode(Double.self, forKey: .lng)
    }

    private init(_ container: KeyedDecodingContainer<CodingKeys2>) throws {
        name = try container.decode(String.self, forKey: .name)
        lat = try container.decode(Double.self, forKey: .lat)
        lng = try container.decode(Double.self, forKey: .lng)
    }
}

也许最后一部分可以使用泛型重写。

这是一个示例,其中 data1 和 data2 是问题中的两个样本

do {

    for data in [data1, data2] {
        let result = try JSONDecoder().decode(Places.self, from: data)
        print(result)
    }
} catch {
    print(error)
}

地点(名称:“02-STATION”,纬度:43.25394,液化天然气:-2.93844)
Places(名称:“Coffey St & Conover St”,纬度:40.677236,液化天然气:-74.015665)

【讨论】:

  • 这看起来很有希望,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多