'struct for parsing google distance json in swif
This is the json output from https://maps.googleapis.com/maps/api/distancematrix/json?origins=rijssen&destinations=almelo&mode=driving&language=en&key=
{ "destination_addresses" : [ "Almelo, Netherlands" ],
"origin_addresses" : [ "Rijssen, Netherlands" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "14.1 km",
"value" : 14090
},
"duration" : {
"text" : "21 mins",
"value" : 1267
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
this is the struct in Swift
struct Model: Codable{
let destination_addresses : [String]
let origin_addresses : [String]
let rows : [Elements]
let status : String
}
struct Elements: Codable {
let elements:[Distance]
}
struct Distance: Codable{
let distance:Value
let duration:Value
let status:String
}
struct Value: Codable{
let text:String
let value:Int
}
This is the api request
guard let url = URL(string: api) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
if let data = data {
let result = try JSONDecoder().decode([Model].self, from: data)
error in compiler
The data couldn’t be read because it isn’t in the correct format.
Why is the code not working? Thank you i.a.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
