Hi all,
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:
I've written two decodable structs to deal with this, however the naming convention of the first item is throwing an error...
I am pretty new to swift, so I could be doing this incorrectly? I guess my biggest question is there a way to have swift ignore the '@' and the '.' items, similar to Java using a @SerializedName() argument?
Thanks.
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:
Code Block { "@odata.context": "$metadata#Member", "value": [ { "MemberKeyNumeric": 123456, "MemberOfficeNumeric": 123456, .... }, { ..... } ] }
I've written two decodable structs to deal with this, however the naming convention of the first item is throwing an error...
Code Block struct wasatchResponse:Decodable{ var @odata.context:String var value:[valueData] } struct valueData:Decodable { var MemberKeyNumeric:Int var MemberOfficeNumeric:Int
I am pretty new to swift, so I could be doing this incorrectly? I guess my biggest question is there a way to have swift ignore the '@' and the '.' items, similar to Java using a @SerializedName() argument?
Thanks.
You should use CodingKeys:
And take care to follow Swift naming conventions:
var starts with lowercase ; struct names with Uppercase.
Tell if that works and what you get when decoding.
Code Block struct ValueData: Decodable { var memberKeyNumeric:Int var memberOfficeNumeric:Int enum CodingKeys : String, CodingKey { case memberKeyNumeric = "MemberKeyNumeric" case memberOfficeNumeric = "MemberOfficeNumeric" } } struct wasatchResponse: Decodable { var context: String var value:[ValueData] enum CodingKeys : String, CodingKey { case context = "@odata.context" case value = "value" } }
And take care to follow Swift naming conventions:
var starts with lowercase ; struct names with Uppercase.
Tell if that works and what you get when decoding.