Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to extract values from string in Ruby on Rails

Posted on 2014-01-18
3
Medium Priority
?
427 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 1500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

876 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