Reputation: 33
Super new to swift, JSON, and pretty all coding so I apologize in advance if this question is redundant to others on the site or I am missing something simple here.
I am looking to return the value of "text" ("1.7 mi") associated with "distance" in the "elements" array in the JSON code below:
{
"destination_addresses" : [ "30 Rockefeller Plaza, New York, NY 10112, USA" ],
"origin_addresses" : [ "352 7th Ave, New York, NY 10001, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1.7 mi",
"value" : 2729
},
"duration" : {
"text" : "15 mins",
"value" : 887
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
I retrieved the JSON data using Alamofire and the Google DistanceMatrix (see my code below), but am having trouble parsing the data to isolate what I need. I know the code below isn't close to what I need, but am unsure as to how to proceed.
func distanceMatrix(startLocation: String, endLocation: String) {
let myOrigin = startLocationTFText
let myDestination = destinationLocationTFText
let url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(myOrigin)&destinations=\(myDestination)&key=API_Key
let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
AF.request(encodedUrl!).responseJSON { response in
print(response.request as Any)
print(response.response as Any)
print(response.data as Any)
print(response.result as Any)
let json = JSON(response.data as Any)
Any help is much appreciated. Thank you.
Upvotes: 1
Views: 614
Reputation: 2164
You can use Decodable
to get the desired result.
struct RootResponse: Decodable {
let destinationAddresses, originAddresses: [String]
let rows: [Rows]
let status: String
}
struct Rows: Decodable {
let elements: [Elements]
}
struct Elements: Decodable {
let distance, duration: Details
let status: String
}
struct Details: Decodable {
let text, value: String
}
This will be your model file and once you have added it then you can go back to your function and use it as:
func distanceMatrix(startLocation: String, endLocation: String) {
let myOrigin = startLocationTFText
let myDestination = destinationLocationTFText
let url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(myOrigin)&destinations=\(myDestination)&key=API_Key"
let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
AF.request(encodedUrl!).responseJSON { response in
print(response.request as Any)
print(response.response as Any)
print(response.data as Any)
print(response.result as Any)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
guard let json = try? decoder.decode(RootResponse.self, from: response.data) else { print("Unable to parse JSON"); return }
print(json)
print(json.rows.first?.elements.first?.distance.value) // This is how you can get the value, but it will be better to safely unwrap them and I have also used first? to get the first object but this is an array and you can always use a for loop for further purpose
}
Upvotes: 1