访问数组中的变量?

时间:2013-02-18 12:51:01

标签: ruby-on-rails arrays soap hash savon

我有一个Savon SOAP会话,我得到一个转换为数组的响应。然后将部分响应存储在包含这样的散列的变量包中;

{
  :cancellation_policies=>nil,
  :contract_id=>"834",
  :hotel_id=>"41298",
  :package_id=>"6b0938e-bf45ed4facda",
  :package_price=>{
    :currency=>"NOK",
    :final_price=>"1400.05",
    :final_price_in_supplier_currency=>"169",
    :original_price=>"1400.05", 
    :original_price_in_supplier_currency=>"169",
    :supplier_currency=>"GBP"
  },
  :rooms=>{
    :room=>{
      :adults_count=>"2",
      :availability=>"Available",
      :description=>{
        :"@xmlns:a"=>"http://schemas.microsoft.com/2003/10/Serialization/Arrays"
      },
      :id=>"6de98b4b-256e-47de-bbd3-700602a16e8e",
      :kids_ages=>{
        :"@xmlns:a"=>"http://schemas.microsoft.com/2003/10/Serialization/Arrays"
      },
      :price=>{
        :currency=>"NOK",
        :final_price=>"1419.05",
        :final_price_in_supplier_currency=>"165",
        :original_price=>"1419.05",
        :original_price_in_supplier_currency=>"165",
        :supplier_currency=>"GBP"
      },
      :room_basis=>"Bed and Breakfast BB",
      :room_basis_original=>"Room and Breakfast (American Buffet Breakfast)", 
      :room_class=>"Standard",
      :room_class_original=>"Double Room",
      :room_type=>"Double",
      :room_type_original=>"Double Room"
    }
  },
  :supplier_id=>"21",
  :supplier_name=>"HTP"
}

当我尝试访问包[:cancellation_policies]时发生奇怪的事情我得到一个错误说;无法将符号转换为整数。

我如何访问例如:cancellation_policies?

package.inspect给出;

{ :cancellation_policies=>nil, :contract_id=>"834", :hotel_id=>"41298", :package_id=>"4e80e642-13eb-48d6-9f1e-8b35f1c406ca", :package_price=>{:currency=>"NOK", :final_price=>"1677.06", :final_price_in_supplier_currency=>"195", :original_price=>"1677.06", :original_price_in_supplier_currency=>"195", :supplier_currency=>"GBP"}, :rooms=>{:room=>{:adults_count=>"2", :availability=>"Available", :description=>{:"@xmlns:a"=>"http://schemas.microsoft.com/2003/10/Serialization/Arrays"}, :id=>"dce10420-bbc5-471e-b388-03d6bb132e27", :kids_ages=>{:"@xmlns:a"=>"http://schemas.microsoft.com/2003/10/Serialization/Arrays"}, :price=>{:currency=>"NOK", :final_price=>"1677.06", :final_price_in_supplier_currency=>"195", :original_price=>"1677.06", :original_price_in_supplier_currency=>"195", :supplier_currency=>"GBP"}, :room_basis=>"Bed and Breakfast BB", :room_basis_original=>"Breakfast (Full Breakfast)", :room_class=>"Standard", :room_class_original=>"Standard Double - Double", :room_type=>"Double", :room_type_original=>"Standard Double - Double"}}, :supplier_id=>"21", :supplier_name=>"HTP"} 

(据我可以说出与上面相同的输出?)

所以我尝试过;

<% package.each do |key, value| %>
<%= key %> is <%= value %>
<% end %>

很好地打印 contract_id是834 hotel_id是41298

......等等,如果这澄清了什么?

这对我来说是暂时的解决方法;

package_as_hash = Hash.new 
package.each do |key, value|
package_as_hash[key]  = value
end 

非常怀疑它是最有效的方式,但至少它有效...

1 个答案:

答案 0 :(得分:1)

由于某种原因,您的包不是哈希值。

如果您在控制台中复制并粘贴以下内容:

package = {
            :level_1=>"1",
            :rooms=>{
              :level_2=>"2",
              :room=>{
                :level_3=>"3",
              }
            },
          }

您将看到您可以访问任何级别的哈希:

package[:level_1]
=> "1" 
package[:rooms][:level_2]
=> "2" 
package[:rooms][:room][:level_3]
=> "3" 

但是,如果您错误地将{更改为[

package = [
            :level_1=>"1",
            :rooms=>{
              :level_2=>"2",
              :room=>{
                :level_3=>"3",
              }
            },
          ]

您将获得Array,它似乎是您所面临的错误:

package[:level_1]
TypeError: can't convert Symbol into Integer

要解决您的问题,如果您希望获得哈希,必须找出原因为什么:

  • 您可以避免将包变成数组;
  • 或者您可以将数组转换为哈希值。但是,要做到这一点,您必须了解阵列的结构。