Swift Codable:使用父母的钥匙作为价值

时间:2019-10-07 09:06:14

标签: swift codable decodable jsondecoder

我有一个在根级别具有ID的JSON:

while IFS= read -r line; do
    [[ $line =~ [[:blank:]]([[:digit:]]+)/([[:digit:]]+)[[:blank:]] ]] && (( ${BASH_REMATCH[1]} != ${BASH_REMATCH[2]} )) && echo "$line"
done < out.txt

我的目标是使用Codable创建具有名称和ID属性的User对象数组。

{
    "12345": {
        "name": "Pim"
    },
    "54321": {
        "name": "Dorien"
    }
}

我知道如何使用Codable with a single root level key,并且知道如何工作with unknown keys。但是我要在这儿做的是两者的结合,我不知道下一步该怎么做。

这是我到目前为止所得到的:(您可以将其粘贴到Playground中)

struct User: Codable {
    let id: String
    let name: String
}

这就是我想要做的:

import UIKit

var json = """
{
    "12345": {
        "name": "Pim"
    },
    "54321": {
        "name": "Dorien"
    }
}
"""

let data = Data(json.utf8)

struct User: Codable {
    let name: String
}

let decoder = JSONDecoder()
do {
    let decoded = try decoder.decode([String: User].self, from: data)
    decoded.forEach { print($0.key, $0.value) }
    // 54321 User(name: "Dorien")
    // 12345 User(name: "Pim")
} catch {
    print("Failed to decode JSON")
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以使用自定义编码密钥并按如下所示设置User来解析未知密钥,

struct CustomCodingKey: CodingKey {

    let intValue: Int?
    let stringValue: String

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

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

struct UserInfo: Codable {
    let name: String
}

struct User: Codable {
    var id: String = ""
    var info: UserInfo?

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CustomCodingKey.self)
        if let key = container.allKeys.first {
            self.id = key.stringValue
            self.info = try container.decode(UserInfo.self, forKey: key)
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CustomCodingKey.self)
        if let key = CustomCodingKey(stringValue: self.id) {
            try container.encode(self.info, forKey: key)
        }
    }
}

let decoder = JSONDecoder()
do {
    let decoded = try decoder.decode(User.self, from: data)
    print(decoded.id) // 12345
    print(decoded.info!.name) // Pim
} catch {
    print("Failed to decode JSON")
}