我已经在外部解析了JSON api,现在它是一个哈希:
["statement", {"generated"=>"2015-01-11", "due"=>"2015-01-25", "period"=>{"from"=>"2015-01-26", "to"=>"2015-02-25"}}
我用过:
require 'json/pure'
require 'open-uri'
def index
content = open("MY_URL").read
@hash = JSON.parse content
render @hash
end
这使用以下内容为视图提供了整个哈希输出:
<% @hash.each do |hash| %>
<%= hash %>
<% end %>
所以我想说我想分别打印生成,到期和期间,我该怎么做呢。我知道的这么近。
数组:
["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 with 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 what she said", "cost"=>9.99}, {"title"=>"Broke back mountain", "cost"=>9.99}], "total"=>24.97}] [:prefixes, ["tasks", "application"]] [:template, "index"]
答案 0 :(得分:1)
简而言之,您需要从哈希中提取值以单独打印它们。
您编写的第一段代码实际上是一个数组,而不是哈希。数组的第一个元素是String,第二个是哈希。您要做的第一件事是创建一个仅包含哈希的变量,删除字符串:
<% @hash_values = @hash[1] %>
使用此哈希,您希望通过按键查找“已生成”,“到期”和“期间”的值来提取它们:
<%= @hash_values['generated'] %>
<%= @hash_values['due'] %>
<%= @hash_values['period'] %>
在'period'的情况下,结果是哈希。许多人想要为格式化提取更多的单个值,例如:
<%= @hash_values['period']['from'] %> to <%= @hash_values['period']['to'] %>