How can I decode the following JSON to a Codable object in Swift?
{"USD":"12.555", "EUR":"11.555"}
Here's the struct i'm using:
struct Prices: Codable {
var USD: Double
var EUR: Double
private enum CodingKeys: String, CodingKey {
case USD = "USD"
case EUR = "EUR"
}
init() {
self.USD = 0.0
self.EUR = 0.0
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.USD = try container.decode(Double.self, forKey: .USD)
self.EUR = try container.decode(Double.self, forKey: .EUR)
}
}
The error I'm getting is
Error typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "USD", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead.", underlyingError: nil))