本文介绍了Rails通过API XML响应遍历哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用Httparty从API获得了这个散列。响应是一个XML,但是httparty显示它是一个散列。但它太深了,得到这些值会令人困惑。 { air_search_result => {xmlns=> http://www.cleartrip.com/air/ , onward_solutions => { solution => [{index=>1, pricing_summary => {base_fare=>3350.0 =>7828.55,total_fare=>11178.55},航班=> {航班=> {=> {段=> [{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 =BLR,arrival_airport=>DEL,arrival_terminal=>3,departure_date_time=>2014-07-06T20:10:00,arrival_date_time=>2014-07 -06T22:40:00,flight_numb er=>404,airline=>AI,operating_airline=>AI,stops=>0,equipment=>320,duration=> 9000}]}}}, pax_pricing_info_list => { pax_pricing_info => {pax_type=>ADT strong>=> { pricing_info => {index=>1,fare_basis_code=>SAP30,SAP30,........ 我需要进入并显示航空公司,departure_airport等。 flights=> {flight= > {segments=> {segment=> <%@flight [air_search_result] [onward_solutions ] [solution]。each do | h | %GT; < strong><%= h [pricing_summary] [total_fare]%>< / strong> - <%= end%> 当我包含[airline]时,rails 4方便地说不能转换String整数。但我可以得到[pricing_summary] [total_fare] 我尝试了许多变化,嵌套循环但可惜,它不起作用。 我也想知道是否有任何宝石或方法,可以直接给出结果而不是如此深的遍历 编辑 <%@flight [air_search_result] [onward_solutions] [solution ] .map do | h | %GT; < strong><%= h [pricing_summary] [total_fare]%>< / strong> - <%h [航班] [航班] [航段] [航段]。 %GT; <%= s [航空公司]%> <%end%>< br> < HR> <%end%> 解决方案查看 BroiSatse :h [flights] [flight] [segments] [segment]是一个数组,不是散列。 $ p> <%@flight [air_search_result] [onward_solutions] [solution]。each do | h | %GT; < strong><%= h [pricing_summary] [total_fare]%>< / strong> - <%= end%> 或第一个 <%h [flights] [flight] [segments] [segment]。first [airline]%> 或全部显示airline H [ 航班] [ 飞行] [ 段] [ 段]地图{|。S | s [airline]} #return:[AI,AI] 或显示连接分隔符 h [flights] [flight] [segments] [segment ] {.MAP | S | s [airline]}。join(',') 或显示更多数据 <%@flight [air_search_result] [onward_solutions] [solution]。each do | h | %GT; < strong><%= h [pricing_summary] [total_fare]%>< / strong> - <%=#{s ['airline']}(#{s ['flight_number']})%> <%end%> <%= end%> 您可以在导轨控制台中检查。部分来自API响应: 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 [首先[航空公司]#'AI' h [航班] [航段] [航段] [航段 ] [segment] [0] [airline]#AI h [flights] [flight] [segments] [segment]。map { | S | s [airline]}。join(',')#AI,AI h [flights] [flight] [segments] [segment]。each {| s | (639)#AI(404) 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 traversingEDIT<% @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 %> 解决方案 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 separatorh["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) 这篇关于Rails通过API XML响应遍历哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 12:15