【问题标题】:Swift How to decode JSON without knowing the key name? [duplicate]Swift 如何在不知道键名的情况下解码 JSON? [复制]
【发布时间】:2019-11-28 21:41:00
【问题描述】:

我的 json 看起来像这样:

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

在我不知道密钥名称的情况下,有什么方法可以解码这种类型的 JSON?

【问题讨论】:

标签: json swift decode decoding


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,我没想到!
猜你喜欢
  • 2019-02-24
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
相关资源
最近更新 更多