I'm trying to parse some JSON response from backend using Codable but always getting a type mismatch.
I've never faced this problem before with Codable.
Here is my JSON:
{
"success": true,
"result": {
"uuid": "ed26b73f-9661-44e0-9975-d841bc533849",
"update_time": "1568035713",
"category_uuid": "b36c1549-c4c9-11e9-8fcd-9151e37ecd87",
"title": "optionaltask",
"description": "",
"reminder_time": 1212121212,
"same_day": 0,
"before_day": 0,
"three_days": 0,
"by_email": 0,
"by_sms": 0,
"by_notification": 0,
"mp3_time": null,
"file_name": null,
"file_link": null,
"status": "0"
},
"error": null
}
And this is my Codable struct:
struct RespElement: Codable {
let success: Bool
let result: Task
let error: String?
}
class Task: Object, Codable {
var uuid, updateTime, categoryUUID, title: String
var resultDescription: String?
var sameDay, beforeDay, threeDays: Int
var bySMS, byNotification: Int
var byEmail: Int
var reminderTime: Int
var mp3Time: Int?
var fileName, fileLink: String?
var status: String
enum CodingKeys: String, CodingKey {
case uuid
case updateTime = "update_time"
case categoryUUID = "category_uuid"
case title
case resultDescription = "description"
case reminderTime = "reminder_time"
case sameDay = "same_day"
case beforeDay = "before_day"
case threeDays = "three_days"
case byEmail = "by_email"
case bySMS = "by_sms"
case byNotification = "by_notification"
case mp3Time = "mp3_time"
case fileName = "file_name"
case fileLink = "file_link"
case status
}
I expect it to be decoded correctly, but I get a weird behavior. Decoder shows an error for Int variables, saying that expected Int but found a string/data. When I change type of variable to String, it says that expected String but found a number instead. When I'm trying to parse it as Bool, it also throws a Type Mismatch.
Task
struct doesn't match the JSON, it's just one piece of it. – rmaddysuccess
andresult
? And even with theconvertFromSnakeCase
strategy the decoder won't decodeby_sms
tobySMS
. – vadianObject
?) – Rob Napier