gpt4 book ai didi

json - swift:如何在不创建包含所述对象数组的结构的情况下解码 json 对象数组?

转载 作者:行者123 更新时间:2023-12-04 08:30:50 25 4
gpt4 key购买 nike

我的数据如下所示:

"places": [
{
"id": 15,
"name": "København",
"typeId": 6,
"coordinates": {
"lat": "55.6760968",
"lng": "12.5683372"
},
"count": 2779
},
{
"id": 19,
"name": "København S",
"typeId": 3,
"coordinates": {
"lat": "55.6508754",
"lng": "12.5991891"
},
"count": 1168
}
]
我希望避免这种情况:
struct Places: Decodable {
let places: [Place]
}
QuickType.io 建议: https://app.quicktype.io?share=j22hopuBnkuHZziOSvxG
而只是在“地点”列表中解码。这样就可以了:
let places = try JSONDecoder().decode([Place].self, from: data)
到目前为止我发现的可能解决方案:
  • “磨损”解决方案:
    https://stackoverflow.com/a/62403633/13481876
  • 创建通用的可解码数组结构:
    https://swiftsenpai.com/swift/decode-dynamic-keys-json/
  • 最佳答案

    如果您发现自己多次需要它,那么您可以构建自己的通用结构,对它找到的任何键进行解码:

    struct Nester<T: Decodable>: Decodable {
    let elements: [T]

    init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    if let key = container.allKeys.first {
    elements = try container.decode([T].self, forKey: key)
    } else {
    // we run into an empty dictionary, let's signal this
    throw DecodingError.typeMismatch([String:Any].self, DecodingError.Context(codingPath: [], debugDescription: "Expected to find at least one key"))
    }
    }

    // A coding key that accepts whatever string value it is given
    struct CodingKeys: CodingKey {
    let stringValue: String
    var intValue: Int? { nil }

    init?(stringValue: String) {
    self.stringValue = stringValue
    }

    init?(intValue: Int) { return nil }
    }
    }
    有了这个,您可以扩展 JSONDecoder为了获得更好的通话站点:
    extension JSONDecoder {
    func decode<T: Decodable>(nested: [T].Type, from data: Data) throws -> [T] {
    try decode(Nester<T>.self, from: data).elements
    }
    }
    然后只需调用新的重载即可:
    let places = try JSONDecoder().decode(nested: [Place].self, from: data)

    附言如果你愿意,你可以在扩展中隐藏复杂的结构,结果是这样的:
    extension JSONDecoder {
    func decode<T: Decodable>(nested: [T].Type, from data: Data) throws -> [T] {
    try decode(Nester<T>.self, from: data).elements
    }

    private struct Nester<T: Decodable>: Decodable {
    let elements: [T]

    init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    if let key = container.allKeys.first {
    elements = try container.decode([T].self, forKey: key)
    } else {
    throw DecodingError.typeMismatch([String:Any].self, DecodingError.Context(codingPath: [], debugDescription: "Expected to find at least one key"))
    }
    }

    struct CodingKeys: CodingKey {
    let stringValue: String
    var intValue: Int? { nil }

    init?(stringValue: String) {
    self.stringValue = stringValue
    }

    init?(intValue: Int) { return nil }
    }
    }
    }
    缺点是如果您想扩展除 JSON 之外的其他解码器,您将无法重用该结构。

    关于json - swift:如何在不创建包含所述对象数组的结构的情况下解码 json 对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65028162/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com