动态字典名称解码器json

时间:2018-03-15 20:44:41

标签: json swift swift4

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))

1 个答案:

答案 0 :(得分:1)

如果您已经知道内部JSON中的所有键,请使用结构来利用静态类型。假设您的JSON(您的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"])
        }
    }
}