My scenario, I am trying to create codable structure for JSON response but I am getting “Type 'Datum' does not conform to protocol 'Decodable' “ error. I made some mistakes in codable but I can’t able to find it. Please check below my response and code and give me the solution.
My Response
{
"status": 200,
"message": "Success",
"country": [
{
"id": 1,
"master": "India",
"type": 2,
"active": 1
},
{
"id": 2,
"master": "US",
"type": 2,
"active": 1
}
],
"cost": 13764,
"data": [
{
"id": 1,
"user_id": 167,
"country": 1,
"card": 1,
"category": 4,
"title": “kms”,
"description": “check”,
"cost": 444,
"attachment": "sample.png",
"create_date": "2019-02-13T00:00:00.000Z",
"device_id": "1111",
"app_type": "Android",
"location": “USA”,
"user": {
"firstname": “Mike”,
"lastname": "P"
},
"app_trans_card": {
"card": “012”
},
"app_trans_master": {
"master": "Domain"
}
}
]
}
My Code
struct Root: Codable {
let status: Int
let message: String
let cost: Int
let data: [Datum]
enum CodingKeys: String, CodingKey {
case status
case message = "message"
case cost
case data
}
}
struct Datum: Codable {
let id, user_id, country, card, category, cost: Int
let title: String
let description: String
let attachment: String
let create_date: String
let device_id: String
let app_type: String
let location: String
let user: Fullname
let app_trans_card: TransactionCard
let app_trans_master: TransactionMaster
enum CodingKeys: String, CodingKey {
case id = "id"
case user_id = "user_id"
case country = "country"
case card = "card"
case category = "category"
case cost = "cost"
case title, description, attachment, create_date, device_id, app_type, location, fullname, transactionCard, transactionMaster
}
}
struct Fullname: Codable {
let firstname, lastname: String
}
struct TransactionCard: Codable {
let card: String
}
struct TransactionMaster: Codable {
let master: String
}