Solved

How to extract values from string in Ruby on Rails

Posted on 2014-01-18
3
400 Views
Last Modified: 2014-01-21
I'm a novice trying to extract the latitude and longitude values (under geometry > location) from the attached JSON.

I tried to use this legacy code without success:
   
    results_array = ActiveSupport::JSON.decode response.body

    if results_array && "OK" == results_array["status"]      
      results_array["results"].each do |result|
        puts logger.info lat = result["geometry"]["location"]["lat"]
   
    end

Open in new window


I figured out how to convert to key-value pairs with:
response_2 = JSON.parse(response.body)

Open in new window

...But still couldn't figure out how to extract the latitude and longitude.

I thought this would be obvious, but didn't find the solution through googling. Thanks a lot for your help.
JSON-text.txt
JSON-text-as-key-value-pairs.txt
0
Comment
Question by:bishop3000
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Andrew Doades earned 500 total points
ID: 39791993
0
 

Author Comment

by:bishop3000
ID: 39798231
Thanks doades, the link got me on the right track.

Here's what I ended up with, which works.

  def get_site_geocoordinates  # Bishop 2014.01.18
    
    geocoder_url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="

    url = geocoder_url + ERB::Util.url_encode( params[:site_address] )
    uri = URI.parse(url)

    response = Net::HTTP.get_response(uri)
    
    geo_text = JSON.parse(response.body)
    


    response_text = ""

    if geo_text && "OK" == geo_text["status"]
      geo_text["results"].each do |result|
        response_text = "Latitude,\"#{result["geometry"]["location"]["lat"]}\"\n"   # entry["geometry"]["location"]["lat"].to_s
        response_text += "Longitude,\"#{result["geometry"]["location"]["lng"]}\"\n"
      end
    else
      response_text = "SiteAddress,\"0\"\n"
    end


    render :text => response_text, :content_type => "text/plain" 
  
  end

Open in new window

0
 
LVL 10

Expert Comment

by:Andrew Doades
ID: 39798237
You're welcome, glad it helped you out :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Call definition from inherited class 2 375
What does << do and mean in Ruby 2 481
Ruby and Rakefile 1 153
Ruby error "undefined method" 6 86
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question