i have problem with my json response
When I use this func , I print all data corect
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let jsonString = String(data: data, encoding: .utf8) {
print(jsonString)
}
}
} .resume()
i get this correct data:
"brand":"Lamborghini",
"model":"Huracan",
"color":"#0052ff"
,"registration":"K1TEST",
"lat":50.244495,
"lng":18.996143,
but when i am using JasonDecoder and Model struct
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
return
}
do {
let decoder = JSONDecoder()
let carData = try decoder.decode([Model].self, from: data)
print(carData)
} catch {
print(error)
}
} .resume()
Model:
struct Model : Codable {
let brand : String
let model : String
let color : String
let registration : String
let lat : Double
let lng : Double }
CodingKeys(stringValue: "lat", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead."
I know that this is something wrong whit lat and lng data but i don’t know how to fix it.
Help
2
Answers
try this. from the error itself we can understand that you are getting lat as string and you are trying to parse as to double. I think the same thing will happen for lng also, so try to parse accordingly
The problem you have identified, where
lat
andlng
can beDouble
and sometimesString
,can be dealt with the following code, to decode
lat
andlng
into a consistentDouble
.