0

I am trying to parse a response from the simplegeo API, I believe it is JSON format? I am able to display the raw content on a page by referencing a variable in that page's controller. Is there a simple way to clean up the response so it is not a jumbled mess?

I currently have this in the "view" of the particular page to display the response:

<%= @simple_geo_client %>
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
soymedia
  • 18
  • 4

1 Answers1

1

I'm guessing you get the answer back as a JSON-string. You can parse this using

api_object = ActiveSupport::JSON.decode(api_response)

This returns the data as a hash object. You can now access the attributes by simply doing api_object["my_attribute"].

Are you not using the official Ruby plugin though? You can find it here.

Maran
  • 2,751
  • 15
  • 12
  • So I have the following in my controller for the page I would like to display the parsed JSON response. When attempting to load the page, I get an error. "Cannot convert hash into string" Any thoughts?

    #class HomeController < ApplicationController def index #SimpleGeo::Client.set_credentials('token', 'secret') #@simple_geo_client = SimpleGeo::Client.get_context(coordinates,coordinates) #api_object = ActiveSupport::JSON.decode(api_response) # end #end

    – soymedia Apr 14 '11 at 14:58