2

私はこのようなハッシュデータを持っています:

{
  "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"
    }
  ]
}

ハッシュ値の一部は単なる文字列であり、一部は次のように1つの要素のみを持つ配列です。

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

ハッシュ内のこのようなすべての要素を次のようにしたいと思います。

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

簡単なRubyメソッド、またはこれを実行できる単一のループはありますか?または、これをフラット化するために、各キーと値のペアをループする必要がありますか?

--更新-9-11-2012

話し合って助けてくれた人たちに感謝します。これが更新です。実際には1つのハッシュ値が配列内に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
4

2 に答える 2

0

単純なループで十分だとは思いません。次のように、再帰を使用する必要があると思います。

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"}}
于 2012-09-10T06:56:08.170 に答える
0

私は@iioiooiooに同意します、あなたは間違いなく再帰を使うべきです。私はこれがあなたが望むことをするべきだと思います:

def collapse(obj)
  return obj unless obj.respond_to? :each
  if obj.is_a? Array
    ary = obj.map{ |elem| collapse elem }
    ary.first if ary.length == 1
  elsif obj.is_a? Hash
    obj.each { |key, val| obj[key] = collapse val }
    obj
  end
end
于 2012-09-10T09:18:44.943 に答える