Weird question tag but still wanted to ask because even though it looks easy no matter what I’ve tried I could not accomplish
I’m trying to parse duration between to points from a URL given by Google Maps directions API. Thanks an answer I’ve received from here I was able to capture the JSON object and get to the duration object however no matter what I did I could not get the inner values “text” or “value” from the “duration” attribute.
Here is the response;
{
"text" : "6 mins",
"value" : 373
}
And here is the code I’ve written in rails
@request = Net::HTTP.get(URI.parse('http://maps.googleapis.com/maps/api/directions/json?origin=40.983204,29.0216549&destination=40.99160908659266,29.02334690093994&sensor=false'))
hash = JSON.parse @request
duration = hash['routes'][0]['legs'][0]['duration']
respond_to do |format|
format.html {render :index}
format.json {render json: duration}
end
Note: Of course, the [0] and [‘text’] methods have been tried.
As a first thing you need to define duration as an instance variable to have it available in your views (if this is where you need to use it)