简体   繁体   English

Rails,如何循环散列数组或仅散列

[英]Rails, how to loop either array of hashes or only a hash

I am connecting to an API and I get array of hashes or only 1 hash for the data. 我正在连接到API,但得到的是哈希数组或数据的1个哈希。 So when the data comes as array of hashes; 因此,当数据以哈希数组的形式出现时;

"extras"=>{"extra"=>[{"id"=>"529216700000100800", "name"=>"Transfer Trogir - Dubrovnik (8 persons max)", "price"=>"290.0", "currency"=>"EUR", "timeunit"=>"0", "customquantity"=>"0", "validdatefrom"=>"1970-01-01", "validdateto"=>"2119-07-20", "sailingdatefrom"=>"1970-01-01", "sailingdateto"=>"2119-07-20", "obligatory"=>"0", "perperson"=>"0", "includedinbaseprice"=>"0", "payableoninvoice"=>"1", "availableinbase"=>"-1", "includesdepositwaiver"=>"0", "includedoptions"=>""}, {"id"=>"528978430000100800", "name"=>"Gennaker + extra deposit (HR)", "price"=>"150.0", "currency"=>"EUR", "timeunit"=>"604800000", "customquantity"=>"0", "validdatefrom"=>"1970-01-01", "validdateto"=>"2119-07-19", "sailingdatefrom"=>"1970-01-01", "sailingdateto"=>"2119-07-19", "obligatory"=>"0", "perperson"=>"0", "includedinbaseprice"=>"0", "payableoninvoice"=>"1", "availableinbase"=>"-1", "includesdepositwaiver"=>"0", "includedoptions"=>""}]

I'am looping through the array to get the values as; 我正在遍历数组以获取值;

b["extras"]["extra"].each do |extra|
  puts extra["id"]
  puts extra["name"]
end

But when this is not array; 但是当这不是数组时; only 1 hash, then this is not working, adding each loop makes it array but not array of hashes; 只有1个散列,那么这是行不通的,添加每个循环会使它成为数组,而不是散列数组;

"extras"=>{"extra"=>{"id"=>"640079840000100800", "name"=>"Comfort package (GRE)", "price"=>"235.0", "currency"=>"EUR", "timeunit"=>"0", "customquantity"=>"0", "validdatefrom"=>"1970-01-01", "validdateto"=>"2120-03-25", "sailingdatefrom"=>"2015-01-01", "sailingdateto"=>"2120-03-25", "obligatory"=>"1", "perperson"=>"0", "includedinbaseprice"=>"0", "payableoninvoice"=>"1", "availableinbase"=>"-1", "includesdepositwaiver"=>"0", "includedoptions"=>""}}


b["extras"]["extra"].each do |extra|
  puts extra["id"]
  puts extra["name"]
end

This time, that gives error TypeError (no implicit conversion of String into Integer); 这次,出现错误TypeError(没有将String隐式转换为Integer);

When I type puts extra.inspect ; 当我输入extra.inspect I get ["id", "640079840000100800"] . 我得到["id", "640079840000100800"] So to make it work I should pass extra[1] to get the id number. 因此,要使其正常工作,我应该传递extra[1]以获得ID号。

But I can not predict either array of hashes or only hash. 但是我无法预测哈希数组或仅哈希。 Is there any easy way to solve this issue that works either array of hashes or just a hash? 有没有一种简单的方法可以解决此问题,该问题要么适用于哈希数组,要么仅适用于哈希?

Naïve solution: one might check the object's type upfront: 朴素的解决方案:可以预先检查对象的类型:

case b["extras"]["extra"]
when Array
    # handle array
when Hash
    # handle hash
end

Proper solution: produce an array of hashes no matter what came. 正确的解决方案:无论发生什么,都产生一系列哈希。

[*[input]].flatten

and deal with it as with an array having at least one hash element (with each .) 并处理它与具有至少一个散列元件的阵列(与each 。)

Please also refer to valuable comment by @Stefan below if you have no allergy using Rails helpers. 如果您对Rails助手没有过敏,也请参考下面@Stefan的宝贵意见。

You could try to use Object#kind_of? 您可以尝试使用Object#kind_of? to determine whether it is an Array or a Hash instance. 确定它是Array实例还是Hash实例。

if b["extras"]["extra"].kind_of? Array
    # handle array
elsif b["extras"]["extra"].kind_of? Hash
    # handle hash
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM