簡體   English   中英

如何在swift 4中解析json字典,每個字典包含具有可解碼協議的不同密鑰?

[英]How to parse a json dictionary in swift 4 , each containing different keys with decodable protocol?

我必須解析具有不同鍵的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
}

1:您可以像使用ObjectSummary(結構車,結構計算機等)那樣構造結構。

2:具有一個“ root”結構,該結構將包括那些作為字段。

3:最后,將該“根”結構發送給解碼器。

您可以像下面的代碼那樣嘗試模型

這將基於我創建的響應數據進行工作。

汽車 將是您的基本模型課程。

struct Cars: Codable { 
    let car: Car
}

struct Car: Codable {
    let objectSummary: CarObjectSummary
    let computer: Computer
    let cat: Cat
    let dog: Dog

    enum CodingKeys: String, CodingKey {
        case objectSummary = "object_summary"
        case computer, cat, dog
    }
}

struct Cat: Codable {
    let objectSummary: CatObjectSummary
    let age, favoriteToy: String
    let image: Image

    enum CodingKeys: String, CodingKey {
        case objectSummary = "object_summary"
        case age
        case favoriteToy = "favorite_toy"
        case image
    }
}

struct Image: Codable {
    let url: String
    let width, height: String
}

struct CatObjectSummary: Codable {
    let type, name, color, description: String
}

struct Computer: Codable {
    let objectSummary: CatObjectSummary
    let purchaseDate: String
    let image: Image

    enum CodingKeys: String, CodingKey {
        case objectSummary = "object_summary"
        case purchaseDate = "purchase_date"
        case image
    }
}

struct Dog: Codable {
    let objectSummary: DogObjectSummary

    enum CodingKeys: String, CodingKey {
        case objectSummary = "object_summary"
    }
}

struct DogObjectSummary: Codable {
    let type, name, color, description: String
    let age, favoriteToy: String
    let image: Image

    enum CodingKeys: String, CodingKey {
        case type, name, color, description, age
        case favoriteToy = "favorite_toy"
        case image
    }
}

struct CarObjectSummary: Codable {
    let type, name, color, description: String
    let doors, price, milage: String
}

我在模型內部做了一些修改,可以檢查一下。 它與您的JSON響應配合得很好。 👍🏻

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM