Solved

How to extract values from string in Ruby on Rails

Posted on 2014-01-18
3
391 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now