Rails基于深度哈希数组中的键显示值

时间:2014-07-05 11:48:42

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

这是使用Httparty从API响应(XML为XML)接收的散列数组。

在内部穿越并获得价值的困难和困惑。

"flights"=>{
    "flight"=>{
        "segments"=>{
            "segment"=>[   (this has square brackets)
                {
                    "index"=>"3",
                    "departure_airport"=>"DEL",
                    "arrival_airport"=>"CCU",
                    "departure_date_time"=>"2014-07-07T13:20:00",
                    "arrival_date_time"=>"2014-07-07T15:35:00",
                    "flight_number"=>"20",
                    "airline"=>"AI",
                    "operating_airline"=>"AI",
                    "stops"=>"0",
                    "equipment"=>"320",
                    "duration"=>"8100"
                },
                {
                    "index"=>"4",
                    "departure_airport"=>"CCU",
                    "arrival_airport"=>"BLR",
                    "departure_date_time"=>"2014-07-07T18:10:00",
                    "arrival_date_time"=>"2014-07-07T20:40:00",
                    "flight_number"=>"771",
                    "airline"=>"AI",
                    "operating_airline"=>"AI",
                    "stops"=>"0",
                    "equipment"=>"319",
                    "duration"=>"9000"
                }
            ]
        }
    }
},

要显示上面的哈希值

<% h["flights"]["flight"]["segments"]["segment"].each do |o,p| %>
      <% if o.class == Hash %> 
          <strong><%= o['airline'] %></strong> 
          <%= o['arrival_airport']  %> - <%= o['arrival_date_time']  %><br>
      <% else %>
          <%= o  %>
      <% end %> 
<% end %>

注意:只需在循环后放置 o['airline'] 即可 can't convert String into Integer

else语句用于解析以下类型的响应。

"flights"=>{
    "flight"=>{
        "segments"=>{
            "segment"=>{     (no square brackets)
                    "index"=>"3",
                    "departure_airport"=>"DEL",
                    "arrival_airport"=>"CCU",
                    "departure_date_time"=>"2014-07-07T13:20:00",
                    "arrival_date_time"=>"2014-07-07T15:35:00",
                    "flight_number"=>"20",
                    "airline"=>"AI",
                    "operating_airline"=>"AI",
                    "stops"=>"0",
                    "equipment"=>"320",
                    "duration"=>"8100"
            }
        }
    }
},

所以有&lt;%= o%&gt;在其他情况下,将给予

["index", "7"] ["departure_airport", "DEL"] ["arrival_airport", "BLR"] ["departure_date_time", "2014-07-10T07:10:00"] ["arrival_date_time", "2014-07-10T09:50:00"] ["flight_number", "807"] ["airline", "9W"] ["operating_airline", "9W"] ["stops", "0"] ["equipment", "738"] ["duration", "9600"]

但是,如果使用<% elsif o=="departure_airport" %> <%= p %> <% end %>代替else语句,则会给出与该键相关联的值。

要使用密钥获取单个值,这很好。但是,将上述格式的所有密钥放入其中以获取其值非常麻烦。

应该有一种更好的方法来解析它,但是无法弄清楚我将如何推断出一个用例[&#39; segment&#39;]会适当地给出结果,基于它是否又是一个哈希或者它只是一把钥匙。

感谢。

2 个答案:

答案 0 :(得分:2)

这里的解决方案是在循环之前将Hash包装成Array

<强>控制器

@segments_array = Array.wrap(h["flights"]["flight"]["segments"]["segment"])

查看

<% @segments_array.each do |segment| %>

  <strong><%= segment['airline'] %></strong> 
  <%= segment['arrival_airport']  %> - <%= segment['arrival_date_time']  %><br>
  ...

<% end %>

答案 1 :(得分:0)

[h["flights"]["flight"]["segments"]["segment"]].flatten.each do |segment|
  puts "#{segment['arrival_airport']} - #{segment['arrival_date_time']}"
end

HTH