Home > Software engineering >  how to decode a json to an struct/class object having space in their keys? [duplicate]
how to decode a json to an struct/class object having space in their keys? [duplicate]

Time:10-04

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

CodePudding user response:

Use CodingKeys to map JSON keys to variables.

    var idNation: String

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