Search code examples
iosjsonswiftobjective-cswift3

how to decode a json to an struct/class object having space in their keys?


Hello I have a json object like this

{
    "ID NATION":  "US"
    "ID YEAR"  :  "1995"
}

I am trying to convert it in to an struct object in swift

struct Details
{
   var ID NATION:
}

Since variables cannot have spaces how can I declare the variables in swift corresponding to keys having spaces in JSON


Solution

  • Use CodingKeys to map JSON keys to variables.

        var idNation: String
    
        private enum CodingKeys : String, CodingKey {
            case idNation = "ID Nation"
        }