[ { "cloudcover" => "100", "humidity" -6ren">
gpt4 book ai didi

ruby - 如何清理哈希和数组混合的数据?

转载 作者:太空宇宙 更新时间:2023-11-03 16:55:04 25 4
gpt4 key购买 nike

我有这样的哈希数据:

{
"current_condition" => [
{
"cloudcover" => "100",
"humidity" => "100",
"observation_time" => "05:44 AM",
"precipMM" => "0.0",
"pressure" => "1015",
"temp_C" => "14",
"temp_F" => "57",
"visibility" => "13",
"weatherCode" => "122",
"weatherDesc" => [
{
"value" => "Overcast"
}
],
"weatherIconUrl" => [
{
"value" => "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
}
],
"winddir16Point" => "NNW",
"winddirDegree" => "340",
"windspeedKmph" => "15",
"windspeedMiles" => "9"
}
],
"request" => [
{
"query" => "94127",
"type" => "Zipcode"
}
],
"weather" => [
{
"date" => "2012-09-09",
"precipMM" => "0.0",
"tempMaxC" => "21",
"tempMaxF" => "69",
"tempMinC" => "12",
"tempMinF" => "53",
"weatherCode" => "113",
"weatherDesc" => [
{
"value" => "Sunny"
}
],
"weatherIconUrl" => [
{
"value" => "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point" => "W",
"winddirDegree" => "279",
"winddirection" => "W",
"windspeedKmph" => "23",
"windspeedMiles" => "14"
},
{
"date" => "2012-09-10",
"precipMM" => "0.1",
"tempMaxC" => "20",
"tempMaxF" => "68",
"tempMinC" => "12",
"tempMinF" => "53",
"weatherCode" => "119",
"weatherDesc" => [
{
"value" => "Cloudy"
}
],
"weatherIconUrl" => [
{
"value" => "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
}
],
"winddir16Point" => "WSW",
"winddirDegree" => "252",
"winddirection" => "WSW",
"windspeedKmph" => "17",
"windspeedMiles" => "11"
}
]
}

有些散列值只是字符串,有些是只有一个元素的数组,如下所示:

"weatherDesc"=>[{"value"=>"Cloudy"}]

我想让散列中的所有元素都像这样:

"weatherDesc"=>{"value"=>"Cloudy"}

是否有一种简单的 Ruby 方法或一个循环可以做到这一点?或者我是否需要循环遍历每个键值对来展平它?

--更新-9-11-2012

感谢那些讨论和帮助我的人。这是一个更新,我刚刚发现数组中实际上有一个哈希值有 2 个对象,我修改了@iioiooioo 在这一行的代码

hash[k] = v.first if v.is_a?( Array ) && v.count == 1

--关于此的更多更新,上面不能正常工作,因为数组中有数组没有被清理,因为具有 2 个元素的数组没有被处理,这将结束对它的递归。我最终这样做了,这并不漂亮但是......

def arr_to_hash(a)
hash = {}
for i in 0..a.length-1
hash[i.to_s] = a[i]
end
hash
end

def clean_it( hash )
hash.each do |k,v|
hash[k] = arr_to_hash v if v.is_a?( Array ) && v.count > 1
hash[k] = v.first if v.is_a?( Array ) && v.count == 1
clean_it( hash[k] ) if hash[k].is_a?( Hash )
end
end

最佳答案

我不相信一个简单的循环就足够了,我认为你需要使用递归,像这样:

a = {"current_condition"=> [{"cloudcover"=>"100", "humidity"=>"100", "observation_time"=>"05:44 AM", "precipMM"=>"0.0", "pressure"=>"1015", "temp_C"=>"14", "temp_F"=>"57", "visibility"=>"13", "weatherCode"=>"122", "weatherDesc"=>[{"value"=>"Overcast"}], "weatherIconUrl"=>[{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"}], "winddir16Point"=>"NNW", "winddirDegree"=>"340", "windspeedKmph"=>"15", "windspeedMiles"=>"9"}], "request"=>[{"query"=>"94127", "type"=>"Zipcode"}], "weather"=>[{"date"=>"2012-09-09", "precipMM"=>"0.0", "tempMaxC"=>"21", "tempMaxF"=>"69", "tempMinC"=>"12", "tempMinF"=>"53", "weatherCode"=>"113", "weatherDesc"=>[{"value"=>"Sunny"}], "weatherIconUrl"=>[{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"}], "winddir16Point"=>"W", "winddirDegree"=>"279", "winddirection"=>"W", "windspeedKmph"=>"23", "windspeedMiles"=>"14"}, {"date"=>"2012-09-10", "precipMM"=>"0.1", "tempMaxC"=>"20", "tempMaxF"=>"68", "tempMinC"=>"12", "tempMinF"=>"53", "weatherCode"=>"119", "weatherDesc"=>[{"value"=>"Cloudy"}], "weatherIconUrl"=>[{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"}], "winddir16Point"=>"WSW", "winddirDegree"=>"252", "winddirection"=>"WSW", "windspeedKmph"=>"17", "windspeedMiles"=>"11"}]}


def clean_it( hash )

hash.each do |k,v|
hash[k] = v.first if v.is_a?( Array )
clean_it( hash[k] ) if hash[k].is_a?( Hash )
end

end

clean_it( a )

p a

输出:

{"current_condition"=>{"cloudcover"=>"100", "humidity"=>"100", "observation_time"=>"05:44 AM", "precipMM"=>"0.0", "pressure"=>"1015", "temp_C"=>"14", "temp_F"=>"57", "visibility"=>"13", "weatherCode"=
>"122", "weatherDesc"=>{"value"=>"Overcast"}, "weatherIconUrl"=>{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"}, "winddir16Point"=>"NNW", "wind
dirDegree"=>"340", "windspeedKmph"=>"15", "windspeedMiles"=>"9"}, "request"=>{"query"=>"94127", "type"=>"Zipcode"}, "weather"=>{"date"=>"2012-09-09", "precipMM"=>"0.0", "tempMaxC"=>"21", "tempMaxF"=>"
69", "tempMinC"=>"12", "tempMinF"=>"53", "weatherCode"=>"113", "weatherDesc"=>{"value"=>"Sunny"}, "weatherIconUrl"=>{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_s
unny.png"}, "winddir16Point"=>"W", "winddirDegree"=>"279", "winddirection"=>"W", "windspeedKmph"=>"23", "windspeedMiles"=>"14"}}

关于ruby - 如何清理哈希和数组混合的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12346379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com