gpt4 book ai didi

json - 动态字典名称解码器 json

转载 作者:行者123 更新时间:2023-11-28 06:02:09 25 4
gpt4 key购买 nike

Swift 4。我的情况与 Using Codable on a dynamic type/object 非常相似但对我来说,变化的变量是字典的名称而不是里面的键。看起来像:

{
"customName": {
"constantKey": Double,
"constantKey2": Double,
}
}

这是我要更改的代码,它已被提议作为另一个问题的答案,我几乎没有做任何更改:

 struct GenericCodingKeys: CodingKey {
var intValue: Int?
var stringValue: String

init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }

static func makeKey(name: String) -> GenericCodingKeys {
return GenericCodingKeys(stringValue: name)!
}
}


struct MyModel: Decodable {
var customName: [String: Double]

private enum CodingKeys: String, CodingKey {
case customName
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

customName = [String: String]()
let subContainer = try container.nestedContainer(keyedBy: GenericCodingKeys.self, forKey: .customName)
for key in subContainer.allKeys {
customName[key.stringValue] = try subContainer.decode(Double.self, forKey: key)
}
}
}

这是我遇到的明显错误,因为我不知道如何更改此自定义名称:keyNotFound(Testapp.MyModel.(CodingKeys in _7A951077E4B6EF2E56D367C5DE0BF0AC).customName, Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot get KeyedDecodingContainer<GenericCodingKeys> -- no value found for key \"customName\"", underlyingError: nil))

最佳答案

如果您已经知道内部 JSON 中的所有键,请使用结构来利用静态类型。假设您的 JSON 的顶层只有 1 个键(您的 customName 键):

struct MyModel: Decodable {
struct InnerModel: Decodable {
var constantKey1: Double
var constantKey2: Double
}

var customName: InnerModel

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: GenericCodingKeys.self)

// Assume that there's only 1 key at the top level in the JSON
if let key = container.allKeys.first {
customName = try container.decode(InnerModel.self, forKey: key)
} else {
throw NSError(domain: NSCocoaErrorDomain, code: 0, userInfo: [NSLocalizedDescriptionKey: "JSON is empty"])
}
}
}

关于json - 动态字典名称解码器 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308859/

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