Swift 4 Decodable - 带有对象的数组

时间:2018-02-18 23:31:03

标签: json swift decodable

我正在使用看起来与此类似的API:

{
   "email": "hello@example.com",
   "settings": [
        {
            "setting_A": {
                "value": -65,
            }
        },
        {
            "setting_B": {
                "value": {
                    "b1": {
                        "val": 12,
                        "unit": "kg"
                    },
                    "b2": {
                        "val": 10,
                        "unit": "g"
                    }
                }
            }
        }
    ]
}

我坚持解码这个JSON结果esp。 settings。有人可以帮我怎么做?

这是我到目前为止所做的:

struct User: Decodable  {
  let email: String
  let settings: [Setting] // not sure how to do this :(
}

1 个答案:

答案 0 :(得分:2)

花了几个小时后,我最终按照@PrashantTukadiya的建议使用了https://app.quicktype.io/#l=swift,并在5分钟内完成了所有操作。我强烈建议您使用这些工具来满足您的编码/解码需求。