5

I am fetching some data from a site using its API-key and some other parameters. I get the data in the form of a JSON object. Now my question is how to use this JSON object in my rails application, maybe doing something like using an instance variable that stores the JSON object then create its erb template which wold display the instance variable's array. I tried going through some of the solutions on Stack Overflow but did not understand them properly, some more basic level help would be great.

Harshit patel
  • 170
  • 11
Jimmy Thakkar
  • 549
  • 4
  • 19

1 Answers1

2

JSON support in Ruby on Rails is provided by the ActiveSupport::JSON, but it wraps JSON gem behind the scene. So you can opt your choice.

Check this example,

@data = [{"id":"7354857336","owner":"49091484@N07","secret":"dc7ce98bf9","server":"8151","farm":9,"title":"01_resize","ispublic":1,"isfriend":0,"isfamily":0},
{"id":"7355453164","owner":"41440187@N07","secret":"b0bc716ee6","server":"7225","farm":8,"title":"{Laduree Macaroons} explore #2","ispublic":1,"isfriend":0,"isfamily":0},
{"id":"7355183126","owner":"38937613@N03","secret":"c342e37a57","server":"7232","farm":8,"title":"REFLECTED AT MIDNIGHT [EXPLORED]","ispublic":1,"isfriend":0,"isfamily":0}]

In .erb file

<% @data.each  do |photo| %>
 <%= photo.id%> ---<%= photo.title %><br></br>
<% end %>
Shamith c
  • 3,719
  • 3
  • 25
  • 33
  • Thanks @Shamith , but i want some more basic understanding on using the JSON object in a instance variable and how to use this instance variable in the view.Some more help would be really helpful.Thanks – Jimmy Thakkar Jun 11 '12 at 04:50
  • Hi Shamith, can you help me out a bit more, i have a JSON object, now i want to use it and display in a view, so how should i add it's methos in a model and how do i call it in a controller's action.Some more help would be great. – Jimmy Thakkar Jun 12 '12 at 13:13