Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JSON object returned from a URL in rails

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.

like image 764
Jimmy Thakkar Avatar asked Nov 03 '22 22:11

Jimmy Thakkar


1 Answers

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 %>
like image 186
Shamith c Avatar answered Nov 08 '22 04:11

Shamith c