the API which I am trying to consume in the iOS application which I am developing is the JSON API. I was using a library in order to parse the data which was being retrieved by the server. Now I am trying to parse the JSON on my own using the new Codable protocol which Swift introduced.
I successfully managed to parse the data and the attributes part but the difficulty I am facing is on the included part.
To start with I created this class:
class UserCodable: Codable {
var data: UserCodableData?
var relationships: UserRelationships?
enum CodingKeys: String, CodingKey {
case data
case relationships = "included"
}
}
in order to store the data retrieved for the User object.
Along with this class, I created these two structures
struct UserCodableData: Codable {
var id: String?
var type: String?
var attributes: UserAttributes?
}
struct UserAttributes: Codable {
var id: String?
var type: String?
var firstName: String?
var lastName: String?
var email: String?
var officePhone: String?
var mobilePhone: String?
var timeZone: String?
var active: Int?
var middleName: String?
var `extension`: String?
var homePhone: String?
var avatar: String?
enum CodingKeys: String, CodingKey {
case id
case type
case firstName = "firstname"
case lastName = "lastname"
case email
case officePhone = "office_phone"
case mobilePhone = "mobile_phone"
case timeZone = "time_zone"
case active
case middleName = "middlename"
case `extension`
case homePhone = "home_phone"
case avatar
}
}
in order to store the data and the attributes appropriately.
Finally I created the structure regarding the relationships (included):
struct UserRelationships: Codable {
var role: RoleCodable?
}
The RoleCodable class follows the same pattern.
The data retrieved regarding the included key is the following:
"data": {
},
"included": [
{
"id": "10",
"type": "roles",
"attributes": {
"title": "Role"
}
}
],
The problem is that the included part contains an array of JSON objects.
How can I decode and initialise the object in the UserRelationships class - in this case the role of type RoleCodable?