Swift 如何在不知道密钥名称的情况下解码 JSON?

Swift How to decode JSON without knowing the key name?

我有 json 看起来像这样:

"events": {
    "1": {
        "id": 1,
        "name": "something"
    },
    "2": {
        "id": 2,
        "name": "something2"
    },...
}

有什么方法可以解码这种我不知道密钥名称的 JSON 吗?

好吧,如果您不知道密钥,您可以将其像字符串一样存储。所以一个解决方案可能是使用像这样的字典:

struct EventsResponse: Codable {
    var events: [String: Event]
}