【问题标题】:How to decode JSON dictionary (with varying keys) of dictionaries, using Codable?如何使用 Codable 解码字典的 JSON 字典(使用不同的键)?
【发布时间】:2019-12-14 09:34:08
【问题描述】:

我有以下简单的 JSON:

{
    "201" :
    {
        "countryName" : "Albania",
        "countryCode" : "AL"
    },
    "202" :
    {
        "countryName" : "Andorra",
        "countryCode" : "AD"
    },
    ...
}

内部字典简单地变成:

struct Mid : Codable
{
    var countryName: String
    var countryCode: String
}

但后来我被困住了。如何表示外部字典?

我想最终得到一个带有String"201""202"、...的Mid 对象字典。

【问题讨论】:

    标签: ios swift dictionary codable


    【解决方案1】:
    • 简单的解决方案:

      • 解码[String:Mid]
    • 如果你想要一个数组,复杂的解决方案:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多