ruby on rails - Iterate obect in view, JSON hash -


i have parsed json api externally , hash such:

["statement", {"generated"=>"2015-01-11", "due"=>"2015-01-25", "period"=>{"from"=>"2015-01-26", "to"=>"2015-02-25"}} 

i used:

require 'json/pure' require 'open-uri'   def index    content = open("my_url").read    @hash = json.parse content    render @hash  end 

and gives me whole hash output on view using:

<% @hash.each |hash| %> <%= hash %> <% end %> 

so lets wanted print generated, due , period, separately, how this. i'm close know it.

array:

 ["statement", {"generated"=>"2015-01-11", "due"=>"2015-01-25", "period"=>{"from"=>"2015-01-26", "to"=>"2015-02-25"}}] ["total", 136.03] ["package", {"subscriptions"=>[{"type"=>"tv", "name"=>"variety movies", "cost"=>50.0}, {"type"=>"talk", "name"=>"talk anytime", "cost"=>5.0}, {"type"=>"broadband", "name"=>"fibre unlimited", "cost"=>16.4}], "total"=>71.4}] ["callcharges", {"calls"=>[{"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"07716393769", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}, {"called"=>"02074351359", "duration"=>"00:23:03", "cost"=>2.13}], "total"=>59.64}] ["store", {"rentals"=>[{"title"=>"50 shades of grey", "cost"=>4.99}], "buyandkeep"=>[{"title"=>"that's said", "cost"=>9.99}, {"title"=>"broke mountain", "cost"=>9.99}], "total"=>24.97}] [:prefixes, ["tasks", "application"]] [:template, "index"] 

in nutshell, need extract values hash print them individually.

the first piece of code wrote array, not hash. first element of array string , second hash. first thing want create variable hash only, removing string:

<% @hash_values = @hash[1] %> 

with hash, want extract values of 'generated', 'due' , 'period' looking them keys:

<%= @hash_values['generated'] %> <%= @hash_values['due'] %> <%= @hash_values['period'] %> 

in case of 'period', result hash. many want extract individual values bit more formatting, e.g.:

<%= @hash_values['period']['from'] %> <%= @hash_values['period']['to'] %> 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -