alphonse
alphonse

Reputation: 717

Parse JSON using codable and CodingKeys swift

I tried to solve with many tutorials, but couldn't.

I have JSON structure like this.

    {
"Region":[
   {
      "head":[
         {
            "total_count":572888
         },
         {
            "RESULT":{
               "CODE":"000",
               "MESSAGE":"Success."
            }
         },
         {
            "api_version":"1.0"
         }
      ]
   },
   {
      "row":[
         {
            "COM_NAME":"company",
            "TYPE_CD":null,
            "BIZ_NM":null,
            "TYPE_NM":null,
            "TELNO":"111-1111-1111",
            "MNY_NM":null,
            "POSBL_YN":null,
            "DATE":"2018-09-30",
            "ROAD_ADDR":"adrress",
            "ZIP_CD":"10303",
            "LOGT":"126.80090346",
            "LAT":"37.673069059",
         }
        ]
    }
]

}

What I want to do is decode some properties from row section.

So I created some structs like this.

struct List: Decodable {
    let Region: [Stores]
}


struct Stores: Decodable {
    let row: [StoreInfo]
}

struct StoreInfo: Decodable {
    let COM_NAME: String?
    let TYPE_NM: String?
    let TELNO: String?
    let DATE: String?
    let ROAD_ADDR: String?
    let LOGT: String?
    let LAT: String?
}

And decode with Jsondecoder like this.

 let decodedData = try JSONDecoder().decode(List.self, from: data)
 print(decodedData)

I get error like this.

    keyNotFound(CodingKeys(stringValue: "Region", intValue: nil),
 Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"Region\", intValue: nil) 
(\"Region\").", underlyingError: nil))

How can I decode correctly?

Upvotes: 0

Views: 380

Answers (2)

Daljeet Seera
Daljeet Seera

Reputation: 198

I used stores and store info as optional and created struct with Codable and it worked.

struct List: Codable {
    let Region: [Stores]?
}


struct Stores: Codable {
    let row: [StoreInfo]?
}

struct StoreInfo: Codable {
    let COM_NAME: String?
    let TYPE_NM: String?
    let TELNO: String?
    let DATE: String?
    let ROAD_ADDR: String?
    let LOGT: String?
    let LAT: String?
}

Upvotes: 0

Joakim Danielson
Joakim Danielson

Reputation: 52053

The array "Region" contains different types, in swift it would be declared as [Any], so you need to declare row as optional so when the decoder finds and tries to decode the element "head" it will just ignore that element and decode the next one.

struct Stores: Decodable {
    let row: [StoreInfo]?
}

PS
The error you posted isn't generated from the code posted, the real error for the posted code is

keyNotFound(CodingKeys(stringValue: "row", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "Region", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"row\", intValue: nil) (\"row\").", underlyingError: nil))

DS

Upvotes: 1

Related Questions