【问题标题】:How to parse a json dictionary in swift 4 , each containing different keys with decodable protocol?如何在 swift 4 中解析一个 json 字典,每个字典都包含具有可解码协议的不同键?
【发布时间】:2018-09-21 21:55:04
【问题描述】:

我必须解析这个具有不同键的 json 字典。 在此示例中,只有 object_summary 键相同,而所有对象中的其他键不同。 我想使用带有 JSONDecoder() 的 Swift 4 Decodable 协议来解析它。请帮忙。

     {
           "car": {
              "object_summary": {
                 "type": "consumer product",
                 "name": "ford",
                 "color": "red",
                 "description": "A car is a wheeled motor vehicle used for transportation."
              "doors": "2",
              "price": "$30000",
              "milage": "100 miles"
            },
           "computer": {
              "object_summary": {
                 "type": "hardware",
                 "name": "mac",
                 "color": "silver",
                 "description": "A computer is a device that can be instructed to carry out sequences of arithmetic or s for looms."
              },
              "purchase_date": "March 4, 2018",
              "image": {
                 "url": "https://seniorsnoworlando.org/wp-content/uploads/2014/05/IMG_0009-1038x576.jpg",
                 "width": "50px",
                 "height": "50px"
              }
            },
            "cat": {
              "object_summary": {
                 "type": "animal",
                 "name": "Max",
                 "color": "orange",
                 "description": "The domestic cat carnivorous mammal."
              },
              "age": "2 years",
              "favorite_toy": "ball",
              "image": {
                 "url": "https://www.petful.com/wp-content/uploads/2016/06/american-shorthair-cat-750x434.jpg",
                 "width": "50px",
                 "height": "50px"
              }
            },
            "dog": {
              "object_summary": {
                 "type": "animal",
                 "name": "Jimmy",
                 "color": "black",
                 "description": "The domestic dog."
              "age": "3 years",
              "favorite_toy": "stuff animal",
              "image": {
                 "url": "https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/13000934/Beagle-On-White-08.jpg",
                 "width": "50px",
                 "height": "50px"
              }
            }
        }

我的数据模型如下所示:

 struct DataModel: Codable{

  let objectsummary:ObjectSummary
  let doors,price, milage, purchasedate, age, favoritetoy: String

  private enum CodingKeys: String, CodingKey {case purchasedate = "purchase_date", favoritetoy = "favorite_toy",objectsummary = "object_summary", doors, price, milage,age}

}
struct ObjectSummary:Codable{
  let type: String
  let name: String
  let color: String
  let description: String
}

【问题讨论】:

  • “car”、“cat”、“dog”键是恒定的还是完全动态的?
  • 它们是不变的。
  • 然后,您可以像使用 ObjectSummary(汽车、猫等)一样简单地构建类(或结构),并拥有一个将这些作为字段包含在内的“根”结构。将该“根”结构发送到解码器。如果出现错误,您可以编辑帖子。
  • 谢谢,我就是这样解决的,而且成功了

标签: ios dictionary swift4 decodable jsondecoder


【解决方案1】:

1:您可以像使用 ObjectSummary 一样构建结构(struct Car、struct Computer 等)。

2:有一个“根”结构,将这些作为字段包含在内。

3:最后,将该“根”结构发送到解码器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多