1

I'm trying to accomplish something really easily but I could not solve it to my beginner level in rails. The question is how do you assign a JSON objects attribute to a rails object.. I'm trying to send a request to Google Map's URL with some parameters. Without JSON parsing it works just fine, however when I try to parse it, I receive many errors. The regular JSON response from Google when I query the URL ([http://maps.googleapis.com/maps/api/directions/json?origin=40.983204,29.0216549&destination=40.99160908659266,29.02334690093994&sensor=false][1]) is like down below;

{
  "routes" : [
    {
      "bounds" : {
        "southwest" : {
          "lat" : 40.98289,
          "lng" : 29.02054
        },
        "northeast" : {
          "lat" : 40.99148,
          "lng" : 29.02388
        }
      },
      "summary" : "Mühürdar Cd",
      "waypoint_order" : [],
      "legs" : [
        {
          "start_location" : {
            "lat" : 40.98322,
            "lng" : 29.02166
          },
          "distance" : {
            "text" : "1.3 km",
            "value" : 1324
          },

And goes on.

The code I have to get the "summary" attribute is;

@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'))
    @result=JSON.parse(@request)["routes"]["summary"]

I wanted to ask what would be the proper way for me to get the summary attribute from the response?

5
  • 1
    From the JSON you've posted, JSON.parse(@request)['routes'] returns an array. So basically, JSON.parse(@request)['routes'][1] should get you the summary. Commented Sep 14, 2012 at 10:17
  • Your method is perfectly fine but I have one more problem. I have written hash['routes'][0]['legs'][0]['duration'] and it gets to a point where I receive a JSON request like { "text" : "6 mins", "value" : 373 }. But I cannot reach neither text or value. What should I add to the end? Commented Sep 14, 2012 at 10:35
  • 1
    hash['routes'][0]['legs'][0]['duration']['text'] would allow you to access Text and ['routes'][0]['legs'][0]['duration']['value'] the Value. Just try 'routes'][0]['legs'][0]['duration'].class to see what type it is and access it accordingly. Cheers. Commented Sep 14, 2012 at 11:07
  • @membLoper I've figured it out by myself. But thanks for your valuable comment. Commented Sep 14, 2012 at 11:10
  • @membLoper post it as answer, please. Commented Oct 2, 2013 at 20:03

1 Answer 1

1
hash['routes'][0]['legs'][0]['duration']['text'] 

would allow you to access Text and

hash['routes'][0]['legs'][0]['duration']['value'] 

the Value. Just try

hash['routes'][0]['legs'][0]['duration'].class 

to see what type it is and access it accordingly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.