Swift Codable: decode dictionary with unknown keys

偶尔善良 提交于 2019-12-11 15:59:52

问题


Codable is great when you know the key formatting of the JSON data. But what if you don't know the keys? I'm currently faced with this problem.

Normally I would expect JSON data to be returned like this:

{
"id": "<123>",
"data": [
    {
        "id": "<id1>",
        "event": "<event_type>",
        "date": "<date>"
    },
    {
        "id": "<id2>",
        "event": "<event_type>",
        "date": "<date>"
    },
]
}

But this is what I'm aiming to decode:

{
"id": "123",
"data": [
    { "<id1>": { "<event>": "<date>" } },
    { "<id2>": { "<event>": "<date>" } },
]
}

Question is: how do I use Codable to decode JSON where the keys are unique? I feel like I'm missing something obvious.

This is what I'm hoping to do so I can use Codable:

struct SampleModel: Codable {
    let id: String
    let data: [[String: [String: Any]]]

    // MARK: - Decoding

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case data = "data"
    }

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

        id = try container.decode(String.self, forKey: .id)
        // This throws an error: Ambiguous reference to member 'decode(_:forKey:)'
        data = try container.decode([[String: [String: Any]]].self, forKey: .data)
    }
}

This throws an error: Ambiguous reference to member 'decode(_:forKey:)'


回答1:


For your completely changed question, the solution is very similar. Your struct simply adds one additional layer above the array. There's no need for any custom decoding nor even any CodingKeys.

Note that you can't use Any in a Codable.

let json="""
{
"id": "123",
"data": [
    { "<id1>": { "<event>": "2019-05-21T16:15:34-0400" } },
    { "<id2>": { "<event>": "2019-07-01T12:15:34-0400" } },
]
}
"""
struct SampleModel: Codable {
    let id: String
    let data: [[String: [String: Date]]]
}

var decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {
    let res = try decoder.decode(SampleModel.self, from: json.data(using: .utf8)!)
    print(res)
} catch {
    print(error)
}

The original answer for your original question.

Since you have an array of nested dictionary where none of the dictionary keys are fixed, and since there are no other fields, you can just decode this as a plain array.

Here's an example:

let json="""
[
    { "<id1>": { "<event>": "2019-07-01T12:15:34-0400" } },
    { "<id2>": { "<event>": "2019-05-21T17:15:34-0400" } },
]
"""
var decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {
    let res = try decoder.decode([[String: [String: Date]]].self, from: json.data(using: .utf8)!)
    print(res)
} catch {
    print(error)
}


来源:https://stackoverflow.com/questions/57262426/swift-codable-decode-dictionary-with-unknown-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!