0

I have to parse this json Dictionary having different keys. In this example only object_summary key is same while the other keys are different in all the objects. I want to parse this using Swift 4 Decodable protocol with JSONDecoder(). Please help.

     {
           "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"
              }
            }
        }

My data model looks like this:

 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
}
Neo123
  • 29
  • 4

1 Answers1

0

1: You can construct the structs like you have with ObjectSummary (struct Car, struct Computer, etc..).

2: Have a 'root' struct that will include those as fields.

3: Finally, send that 'root' struct to the decoder.

realtimez
  • 2,525
  • 2
  • 20
  • 24