0

I seem to be having trouble parsing a JSON response from a server that has a unique naming convention for the first item in the object:

{
    "@odata.context": "$metadata#Member",
    "value": [
            {
                "MemberKeyNumeric": 123456,
                "MemberOfficeNumeric": 123456,
                ....
            },
            {
            .....
            }
    ]}

I've written two decodable structs to deal with the data, however the naming convention of the '@odata.context' item is throwing an error in swift:

struct wasatchResponse:Decodable{
    var @odata.context:String
    var value:[valueData]
}
struct valueData:Decodable {
    var MemberKeyNumeric:Int
    var MemberOfficeNumeric:Int

I'm pretty new to swift, so I could be doing this incorrectly? I guess my biggest question here is there a way to have swift ignore the '@' and the '.' items, similar to RETROFIT in Java using the @SerializedName() argument?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • Create a CodingKey enum to map the property names between your struct and the json. See [this article](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types) for instance – Joakim Danielson Jan 03 '21 at 17:08
  • Also, property names should start with a lowercase letters and it increases readability to use spaces as well like between the : and the type. – Joakim Danielson Jan 03 '21 at 17:12
  • @JoakimDanielson The CodingKey enum worked great, thank you again! I'm slowly stumbling on all the different conventions haha, brand new to mobile development. – Julian Jerzerick Jan 03 '21 at 17:41

0 Answers0