【问题标题】:Iterate obect in view, JSON hash迭代视图中的对象,JSON 哈希
【发布时间】:2015-07-26 17:16:28
【问题描述】:

我已经在外部解析了 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"]

【问题讨论】:

    标签: ruby-on-rails arrays parsing loops hash


    【解决方案1】:

    简而言之,您需要从哈希中提取值以单独打印它们。

    您编写的第一段代码实际上是一个数组,而不是哈希。数组的第一个元素是字符串,第二个元素是散列。您要做的第一件事是仅使用哈希创建一个变量,然后删除字符串:

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

    使用此哈希,您希望通过键查找来提取“生成”、“到期”和“期间”的值:

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

    对于“句号”,结果是一个哈希。你们很多人想要更多地提取单个值以进行格式化,例如:

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

    【讨论】:

    • 一切对我来说都有意义,但是我得到一个错误 undefined method `[]' for nil:NilClass
    • 能否复制并粘贴引发错误的代码行?
    • 应用程序跟踪:app/views/tasks/index.html.erb:3:in block in _app_views_tasks_index_html_erb___299787190_93358320' app/views/tasks/index.html.erb:1:in each' app/views/tasks/index .html.erb:1:in _app_views_tasks_index_html_erb___299787190_93358320' app/controllers/tasks_controller.rb:13:in index'
    • 你能打印出hash_values,然后重新发送第一段代码吗?不确定您问题中的第一段代码(我告诉您从中提取 @hash_values 的代码)是因为您没有关闭数组。
    • 我的错,我没有看到正确的数组,数组实际上真的很大。它在结尾处被关闭,其前缀也存在。我已经编辑了代码,请看一下。
    猜你喜欢
    • 2012-12-27
    • 2020-11-20
    • 2013-08-23
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2019-04-06
    相关资源
    最近更新 更多