0
votes

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.

1
Show your use of JSONDecoder. Also note that you single Task struct doesn't match the JSON, it's just one piece of it.rmaddy
I expect it to be decoded correctly. No, it isn't. Where is the struct with the members success and result? And even with the convertFromSnakeCase strategy the decoder won't decode by_sms to bySMS.vadian
updated my code.Doni Zu
You should show the full json response that you are trying to parse, especially the response that is causing you an error. Only showing part of it makes it harder to diagnose what is wrong. You should also show, as @maddy suggested, the JSONDecoder that you are using.Andrew
I can't reproduce this with your above JSON and code. It decodes as you expect. That suggests that the JSON you've given here isn't actually what's going into the decoder, or that you have a custom decoder somewhere (perhaps as part of Object?)Rob Napier

1 Answers

-1
votes

Your Struct Should be like this :

class Task: Codable { var success: Bool var result: [Result] var error: String? }

class Result: Codable
{

var sameDay: Int
var beforeDay: Int
var threeDays: Int
var by_email: Int
var by_sms: Int
var by_notification: Int

}

try this