Rails traverse hash from API XML response

2019-07-31 05:54发布

问题:

I have this hash that I received from an API, using Httparty. The response was an XML but httparty showed it as a hash. But it is soo deep, it gets confusing to get the values.

{"air_search_result"=>{"xmlns"=>"http://www.cleartrip.com/air/", "onward_solutions"=>{"solution"=>[{"index"=>"1", "pricing_summary"=>{"base_fare"=>"3350.0", "taxes"=>"7828.55", "total_fare"=>"11178.55"}, "flights"=>{"flight"=>{"segments"=>{"segment"=>[{"index"=>"1", "departure_airport"=>"BOM", "arrival_airport"=>"BLR", "departure_date_time"=>"2014-07-06T09:15:00", "arrival_date_time"=>"2014-07-06T10:50:00", "flight_number"=>"639", "airline"=>"AI", "operating_airline"=>"AI", "stops"=>"0", "equipment"=>"319", "duration"=>"5700"}, {"index"=>"2", "departure_airport"=>"BLR", "arrival_airport"=>"DEL", "arrival_terminal"=>"3", "departure_date_time"=>"2014-07-06T20:10:00", "arrival_date_time"=>"2014-07-06T22:40:00", "flight_number"=>"404", "airline"=>"AI", "operating_airline"=>"AI", "stops"=>"0", "equipment"=>"320", "duration"=>"9000"}]}}}, "pax_pricing_info_list"=>{"pax_pricing_info"=>{"pax_type"=>"ADT", "pricing_info_list"=>{"pricing_info"=>{"index"=>"1", "fare_basis_code"=>"SAP30,SAP30", ........

I need to get inside and show airline, departure_airport, etc.

"flights"=>{"flight"=>{"segments"=>{"segment"=>

<% @flight["air_search_result"]["onward_solutions"]["solution"].each do|h| %>
<strong><%=h["pricing_summary"]["total_fare"] %></strong> - 
<% h["flights"]["flight"]["segments"]["segment"] %>
<%= end %>

When I include ["airline"], rails 4 conviniently says can't convert String into Integer. But I can get ["pricing_summary"]["total_fare"]
I tried many variations, nested loops but alas, it does not work.

I am also wondering if there is any gem or method which can directly give the result instead of such deep traversing

EDIT

<% @flight["air_search_result"]["onward_solutions"]["solution"].map do|h| %>
  <strong><%=h["pricing_summary"]["total_fare"] %></strong> - 
  <% h["flights"]["flight"]["segments"]["segment"].map do |s| %>
    <%= s["airline"] %>
  <% end %><br> <hr>
<% end %>

回答1:

see the comment from "BroiSatse": "h["flights"]["flight"]["segments"]["segment"] is an array, not a hash."

<% @flight["air_search_result"]["onward_solutions"]["solution"].each do|h| %>
  <strong><%=h["pricing_summary"]["total_fare"] %></strong> - 
  <%= h["flights"]["flight"]["segments"]["segment"][0]["airline"] %>
<%= end %>

or with first

<% h["flights"]["flight"]["segments"]["segment"].first["airline"] %>

or show all "airline"

h["flights"]["flight"]["segments"]["segment"].map{|s| s["airline"]}
# return: ["AI", "AI"]

or show join separator

h["flights"]["flight"]["segments"]["segment"].map{|s| s["airline"]}.join(', ')

or show more data

<% @flight["air_search_result"]["onward_solutions"]["solution"].each do|h| %>
  <strong><%=h["pricing_summary"]["total_fare"] %></strong> - 
  <% h["flights"]["flight"]["segments"]["segment"].each do |s| %>
    <%= "#{s['airline']} (#{s['flight_number']})" %>
  <% end %>
<%= end %>

You can check in the rails console. Part from API response:

h= {"flights"=>
  {"flight"=>
    {"segments"=>
      {"segment"=>
        [{"equipment"=>"319",
          "stops"=>"0",
          "duration"=>"5700",
          "departure_date_time"=>"2014-07-06T09:15:00",
          "index"=>"1",
          "airline"=>"AI",
          "flight_number"=>"639",
          "arrival_date_time"=>"2014-07-06T10:50:00",
          "arrival_airport"=>"BLR",
          "departure_airport"=>"BOM",
          "operating_airline"=>"AI"},
         {"equipment"=>"320",
          "stops"=>"0",
          "duration"=>"9000",
          "departure_date_time"=>"2014-07-06T20:10:00",
          "arrival_terminal"=>"3",
          "index"=>"2",
          "airline"=>"AI",
          "flight_number"=>"404",
          "arrival_date_time"=>"2014-07-06T22:40:00",
          "arrival_airport"=>"DEL",
          "departure_airport"=>"BLR",
          "operating_airline"=>"AI"}]}}},
 "index"=>"1",
 "pricing_summary"=>
  {"taxes"=>"7828.55", "base_fare"=>"3350.0", "total_fare"=>"11178.55"}}


h["flights"]["flight"]["segments"]["segment"].first["airline"] # 'AI'

h["flights"]["flight"]["segments"]["segment"][0]["airline"] # "AI"

h["flights"]["flight"]["segments"]["segment"].map{|s| s["airline"]}.join(', ') # "AI, AI"

h["flights"]["flight"]["segments"]["segment"].each { |s| puts "#{s['airline']} (#{s['flight_number']})" }
# AI (639)
# AI (404)