Hi, I have a problem when trying to parse the JSON which used to work before but it is broken now and I have no idea why it is.
JSON data:
{
"success": true,
"data": {
"email": "example@gmail.com",
"password": "$2b$10$flI1flm1Ptas33z3uBct6.UkhgiRmv/qIzJCVe2PIUycUFIh0BZfW",
"favouriteRestaurants": [],
"favouriteDishes": [],
"_id": "655adf2386348c2be2360852",
"reservations": [],
"createdAt": "2023-11-20T04:22:59.831Z",
"updatedAt": "2023-11-20T04:22:59.831Z",
"__v": 0
}
}
And my model is
struct SignupResponse: Decodable {
let email: String?
let password: String?
let id: String?
private enum CodingKeys: String, CodingKey {
case data
case email
case password
case id = "_id"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let dataContainer = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
email = try dataContainer.decodeIfPresent(String.self, forKey: .email)
password = try dataContainer.decodeIfPresent(String.self, forKey: .password)
id = try dataContainer.decodeIfPresent(String.self, forKey: .id)
}
I couldn't figure out the reason. Thank you for your help!
If an error is getting thrown, can you provide the exact error message? Or is something else going wrong? Just “it is broken” is not helpful for troubleshooting.
And what do “before” and “now” mean in terms of OS version, Xcode version, anything else that may have changed?
BTW, I tried out your code here (Xcode 14.3.1, iOS 17 simulator) and it actually works fine for me.