-5
你會希望通過哈希遞歸,這裏是一個遞歸方法: -迭代散列/陣列組合
def ihash(h)
h.each_pair do |k,v|
if v.is_a?(Hash)
puts "key: #{k} recursing..."
ihash(v)
else
# MODIFY HERE! Look for what you want to find in the hash here
puts "key: #{k} value: #{v}"
end
end
end
然後你可以採取任何哈希,並傳遞它:
h = {
"x" => "a",
"y" => {
"y1" => {
"y2" => "final"
},
"yy1" => "hello"
}}
ihash(h)
但這下面哈希/陣列/散列組合不是與上面的代碼工作
{"status"=>"REQUEST_SUCCEEDED", "responseTime"=>27, "message"=>[], "Results"=>{"series"=>[{"seriesID"=>"LAUCN040010000000005", "data"=>[{"year"=>"2015", "period"=>"M06", "periodName"=>"June", "value"=>"18444", "footnotes"=>[{"code"=>"P", "text"=>"Preliminary."}]}, {"year"=>"2015", "period"=>"M05", "periodName"=>"May", "value"=>"18443", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M04", "periodName"=>"April", "value"=>"18012", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M03", "periodName"=>"March", "value"=>"17701", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M02", "periodName"=>"February", "value"=>"17549", "footnotes"=>[{}]}, {"year"=>"2015", "period"=>"M01", "periodName"=>"January", "value"=>"17632", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M12", "periodName"=>"December", "value"=>"17631", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M11", "periodName"=>"November", "value"=>"17668", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M10", "periodName"=>"October", "value"=>"17754", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M09", "periodName"=>"September", "value"=>"18095", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M08", "periodName"=>"August", "value"=>"18219", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M07", "periodName"=>"July", "value"=>"17860", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M06", "periodName"=>"June", "value"=>"18054", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M05", "periodName"=>"May", "value"=>"18011", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M04", "periodName"=>"April", "value"=>"17737", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M03", "periodName"=>"March", "value"=>"17436", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M02", "periodName"=>"February", "value"=>"17594", "footnotes"=>[{}]}, {"year"=>"2014", "period"=>"M01", "periodName"=>"January", "value"=>"17562", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M12", "periodName"=>"December", "value"=>"17576", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M11", "periodName"=>"November", "value"=>"17251", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M10", "periodName"=>"October", "value"=>"17416", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M09", "periodName"=>"September", "value"=>"18010", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M08", "periodName"=>"August", "value"=>"18337", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M07", "periodName"=>"July", "value"=>"17840", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M06", "periodName"=>"June", "value"=>"18205", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M05", "periodName"=>"May", "value"=>"18307", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M04", "periodName"=>"April", "value"=>"17950", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M03", "periodName"=>"March", "value"=>"17736", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M02", "periodName"=>"February", "value"=>"17820", "footnotes"=>[{}]}, {"year"=>"2013", "period"=>"M01", "periodName"=>"January", "value"=>"17956", "footnotes"=>[{}]}]}]}}
你的問題是什麼? – sawa