开发者

How do I properly parse and display a JSON response from a variable in a Ruby controller?

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 %>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜