res.read_body.to_json
=> "\"{\\\"publishingOffice\\\":\\\"青森地方気象台\\\",\\\"reportDatetime\\\":\\\"2022-10-30T10:31:00+09:00\\\",\\\"targetArea\\\":\\\"青森県\\\",\\\"headlineText\\\":\\\"\\\",\\\"text\\\":\\\"北日本は気圧の谷となっています。一方、中国東北区に高気圧があって、本州付近に張り出しています。\\\\n\\\\n青森県は、曇りや晴れで、雨の降っている所があります。\\\\n\\\\n30日は、気圧の谷や寒気の影響により、曇りや晴れで、雨や雷雨の所があるでしょう。\\\\n\\\\n31日は、高気圧に覆われるため、晴れや曇りの見込みです。\\\\n\\\\n<天気変化等の留意点>\\\\n30日は、津軽では、落雷や突風に注意してください。\\\"}\""
ここまではよかった。
でもここから抽出できると思ったが、ここからできなかった。
上のページで試しながら理想の形になるまで実験をし続けたが解決法が見つからなかった。
たまたま見かけたこれ試してみた。
関連記事(理想の形)
解法 文字列を
irb(main):034:0> JSON.parse(res.body)
=>
[{"publishingOffice"=>"青森地方気象台",
"reportDatetime"=>"2022-10-31T11:00:00+09:00",
"timeSeries"=>
[{"timeDefines"=>["2022-10-31T11:00:00+09:00", "2022-11-01T00:00:00+09:00", "2022-11-02T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]},
{"area"=>{"name"=>"下北", "code"=>"020020"},
"weatherCodes"=>["111", "200", "200"],
"weathers"=>["晴れ 夕方 から くもり", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり"],
"winds"=>["東の風 後 南東の風", "南の風 海上 では 南の風 やや強く", "西の風"],
"waves"=>["1.5メートル 後 2メートル", "2メートル", "1.5メートル"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"},
"weatherCodes"=>["200", "200", "201"],
"weathers"=>["くもり 所により 昼前 雨", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり 時々 晴れ"],
"winds"=>["東の風 後 南の風", "南の風 後 南西の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["2メートル 後 2.5メートル うねり を伴う", "2.5メートル 後 2メートル うねり を伴う", "1.5メートル"]}]},
{"timeDefines"=>
["2022-10-31T12:00:00+09:00",
"2022-10-31T18:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T06:00:00+09:00",
"2022-11-01T12:00:00+09:00",
"2022-11-01T18:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"}, "pops"=>["0", "0", "0", "0", "10", "50"]},
{"area"=>{"name"=>"下北", "code"=>"020020"}, "pops"=>["10", "10", "10", "10", "10", "30"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"}, "pops"=>["10", "10", "10", "10", "10", "30"]}]},
{"timeDefines"=>
["2022-10-31T09:00:00+09:00",
"2022-10-31T00:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T09:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"}, "temps"=>["17", "17", "7", "19"]},
{"area"=>{"name"=>"八戸", "code"=>"31602"}, "temps"=>["14", "14", "7", "17"]},
{"area"=>{"name"=>"むつ", "code"=>"31111"}, "temps"=>["16", "16", "6", "17"]},
{"area"=>{"name"=>"深浦", "code"=>"31436"}, "temps"=>["16", "16", "9", "19"]},
{"area"=>{"name"=>"弘前", "code"=>"31461"}, "temps"=>["17", "17", "6", "19"]}]}]},
{"publishingOffice"=>"青森地方気象台",
"reportDatetime"=>"2022-10-31T11:00:00+09:00",
"timeSeries"=>
[{"timeDefines"=>
["2022-11-01T00:00:00+09:00",
"2022-11-02T00:00:00+09:00",
"2022-11-03T00:00:00+09:00",
"2022-11-04T00:00:00+09:00",
"2022-11-05T00:00:00+09:00",
"2022-11-06T00:00:00+09:00",
"2022-11-07T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["112", "202", "202", "202", "200", "200", "201"],
"pops"=>["", "60", "60", "60", "40", "40", "30"],
"reliabilities"=>["", "", "B", "C", "B", "C", "B"]},
{"area"=>{"name"=>"下北・三八上北", "code"=>"020200"},
"weatherCodes"=>["200", "201", "202", "200", "201", "201", "101"],
"pops"=>["", "30", "50", "40", "30", "30", "20"],
"reliabilities"=>["", "", "C", "B", "A", "B", "A"]}]},
{"timeDefines"=>
["2022-11-01T00:00:00+09:00",
"2022-11-02T00:00:00+09:00",
"2022-11-03T00:00:00+09:00",
"2022-11-04T00:00:00+09:00",
"2022-11-05T00:00:00+09:00",
"2022-11-06T00:00:00+09:00",
"2022-11-07T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"},
"tempsMin"=>["", "9", "7", "6", "5", "6", "5"],
"tempsMinUpper"=>["", "11", "9", "8", "7", "8", "8"],
"tempsMinLower"=>["", "8", "6", "4", "2", "4", "3"],
"tempsMax"=>["", "17", "13", "11", "11", "12", "13"],
"tempsMaxUpper"=>["", "19", "17", "13", "13", "14", "16"],
"tempsMaxLower"=>["", "15", "12", "10", "8", "9", "11"]},
{"area"=>{"name"=>"八戸", "code"=>"31602"},
"tempsMin"=>["", "8", "6", "5", "4", "5", "5"],
"tempsMinUpper"=>["", "9", "8", "7", "5", "7", "8"],
"tempsMinLower"=>["", "7", "5", "4", "2", "4", "3"],
"tempsMax"=>["", "17", "14", "11", "11", "12", "13"],
"tempsMaxUpper"=>["", "19", "17", "13", "13", "14", "16"],
"tempsMaxLower"=>["", "15", "12", "9", "8", "9", "10"]}]}],
"tempAverage"=>
{"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"}, "min"=>"5.7", "max"=>"14.5"},
{"area"=>{"name"=>"八戸", "code"=>"31602"}, "min"=>"5.4", "max"=>"14.9"}]},
"precipAverage"=>
{"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"}, "min"=>"16.3", "max"=>"30.5"},
{"area"=>{"name"=>"八戸", "code"=>"31602"}, "min"=>"3.2", "max"=>"9.4"}]}}]
調べていた時に見かけた理想の形になった。
やったー!
運がよかった。本当は考えてできた方がよかったのかもしれないけど...。
抽出作業に悪戦苦闘
irb(main):036:0> b[0]
=>
{"publishingOffice"=>"青森地方気象台",
"reportDatetime"=>"2022-10-31T11:00:00+09:00",
"timeSeries"=>
[{"timeDefines"=>
["2022-10-31T11:00:00+09:00", "2022-11-01T00:00:00+09:00", "2022-11-02T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]},
{"area"=>{"name"=>"下北", "code"=>"020020"},
"weatherCodes"=>["111", "200", "200"],
"weathers"=>["晴れ 夕方 から くもり", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり"],
"winds"=>["東の風 後 南東の風", "南の風 海上 では 南の風 やや強く", "西の風"],
"waves"=>["1.5メートル 後 2メートル", "2メートル", "1.5メートル"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"},
"weatherCodes"=>["200", "200", "201"],
"weathers"=>["くもり 所により 昼前 雨", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり 時々 晴れ"],
"winds"=>["東の風 後 南の風", "南の風 後 南西の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["2メートル 後 2.5メートル うねり を伴う", "2.5メートル 後 2メートル うねり を伴う", "1.5メートル"]}]},
{"timeDefines"=>
["2022-10-31T12:00:00+09:00",
"2022-10-31T18:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T06:00:00+09:00",
"2022-11-01T12:00:00+09:00",
"2022-11-01T18:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"}, "pops"=>["0", "0", "0", "0", "10", "50"]},
{"area"=>{"name"=>"下北", "code"=>"020020"}, "pops"=>["10", "10", "10", "10", "10", "30"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"}, "pops"=>["10", "10", "10", "10", "10", "30"]}]},
{"timeDefines"=>
["2022-10-31T09:00:00+09:00",
"2022-10-31T00:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T09:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"}, "temps"=>["17", "17", "7", "19"]},
{"area"=>{"name"=>"八戸", "code"=>"31602"}, "temps"=>["14", "14", "7", "17"]},
{"area"=>{"name"=>"むつ", "code"=>"31111"}, "temps"=>["16", "16", "6", "17"]},
{"area"=>{"name"=>"深浦", "code"=>"31436"}, "temps"=>["16", "16", "9", "19"]},
{"area"=>{"name"=>"弘前", "code"=>"31461"}, "temps"=>["17", "17", "6", "19"]}]}]}
irb(main):037:0> b[publishingOffice]
(irb):37:in `<main>': undefined local variable or method `publishingOffice' for main:Object (NameError)
b[publishingOffice]
^^^^^^^^^^^^^^^^
irb(main):038:0> b["publishingOffice"]
(irb):38:in `[]': no implicit conversion of String into Integer (TypeError)
irb(main):039:0> b['publishingOffice']
(irb):39:in `[]': no implicit conversion of String into Integer (TypeError)
irb(main):040:0> b.class
=> Array
irb(main):041:0> b[0][1]
=> nil
irb(main):042:0> b[0]
=>
{"publishingOffice"=>"青森地方気象台",
"reportDatetime"=>"2022-10-31T11:00:00+09:00",
"timeSeries"=>
[{"timeDefines"=>
["2022-10-31T11:00:00+09:00", "2022-11-01T00:00:00+09:00", "2022-11-02T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]},
{"area"=>{"name"=>"下北", "code"=>"020020"},
"weatherCodes"=>["111", "200", "200"],
"weathers"=>["晴れ 夕方 から くもり", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり"],
"winds"=>["東の風 後 南東の風", "南の風 海上 では 南の風 やや強く", "西の風"],
"waves"=>["1.5メートル 後 2メートル", "2メートル", "1.5メートル"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"},
"weatherCodes"=>["200", "200", "201"],
"weathers"=>["くもり 所により 昼前 雨", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり 時々 晴れ"],
"winds"=>["東の風 後 南の風", "南の風 後 南西の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["2メートル 後 2.5メートル うねり を伴う", "2.5メートル 後 2メートル うねり を伴う", "1.5メートル"]}]},
{"timeDefines"=>
["2022-10-31T12:00:00+09:00",
"2022-10-31T18:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T06:00:00+09:00",
"2022-11-01T12:00:00+09:00",
"2022-11-01T18:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"}, "pops"=>["0", "0", "0", "0", "10", "50"]},
{"area"=>{"name"=>"下北", "code"=>"020020"}, "pops"=>["10", "10", "10", "10", "10", "30"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"}, "pops"=>["10", "10", "10", "10", "10", "30"]}]},
{"timeDefines"=>
["2022-10-31T09:00:00+09:00",
"2022-10-31T00:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T09:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"}, "temps"=>["17", "17", "7", "19"]},
{"area"=>{"name"=>"八戸", "code"=>"31602"}, "temps"=>["14", "14", "7", "17"]},
{"area"=>{"name"=>"むつ", "code"=>"31111"}, "temps"=>["16", "16", "6", "17"]},
{"area"=>{"name"=>"深浦", "code"=>"31436"}, "temps"=>["16", "16", "9", "19"]},
{"area"=>{"name"=>"弘前", "code"=>"31461"}, "temps"=>["17", "17", "6", "19"]}]}]}
irb(main):043:0> b[0][0]
=> nil
irb(main):044:0> b[0]['publishingOffice']
=> "青森地方気象台"
irb(main):045:0> b[0]['timeSeries']
=>
[{"timeDefines"=>["2022-10-31T11:00:00+09:00", "2022-11-01T00:00:00+09:00", "2022-11-02T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]},
{"area"=>{"name"=>"下北", "code"=>"020020"},
"weatherCodes"=>["111", "200", "200"],
"weathers"=>["晴れ 夕方 から くもり", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり"],
"winds"=>["東の風 後 南東の風", "南の風 海上 では 南の風 やや強く", "西の風"],
"waves"=>["1.5メートル 後 2メートル", "2メートル", "1.5メートル"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"},
"weatherCodes"=>["200", "200", "201"],
"weathers"=>["くもり 所により 昼前 雨", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり 時々 晴れ"],
"winds"=>["東の風 後 南の風", "南の風 後 南西の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["2メートル 後 2.5メートル うねり を伴う", "2.5メートル 後 2メートル うねり を伴う", "1.5メートル"]}]},
{"timeDefines"=>
["2022-10-31T12:00:00+09:00",
"2022-10-31T18:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T06:00:00+09:00",
"2022-11-01T12:00:00+09:00",
"2022-11-01T18:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"}, "pops"=>["0", "0", "0", "0", "10", "50"]},
{"area"=>{"name"=>"下北", "code"=>"020020"}, "pops"=>["10", "10", "10", "10", "10", "30"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"}, "pops"=>["10", "10", "10", "10", "10", "30"]}]},
{"timeDefines"=>
["2022-10-31T09:00:00+09:00",
"2022-10-31T00:00:00+09:00",
"2022-11-01T00:00:00+09:00",
"2022-11-01T09:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"青森", "code"=>"31312"}, "temps"=>["17", "17", "7", "19"]},
{"area"=>{"name"=>"八戸", "code"=>"31602"}, "temps"=>["14", "14", "7", "17"]},
{"area"=>{"name"=>"むつ", "code"=>"31111"}, "temps"=>["16", "16", "6", "17"]},
{"area"=>{"name"=>"深浦", "code"=>"31436"}, "temps"=>["16", "16", "9", "19"]},
{"area"=>{"name"=>"弘前", "code"=>"31461"}, "temps"=>["17", "17", "6", "19"]}]}]
irb(main):046:0> b[0]['timeSeries']['areas']
(irb):46:in `[]': no implicit conversion of String into Integer (TypeError)
irb(main):047:0> b[0]['timeSeries']['timeDefines']
(irb):47:in `[]': no implicit conversion of String into Integer (TypeError)
irb(main):048:0> b[0]['timeSeries'][0]
=>
{"timeDefines"=>["2022-10-31T11:00:00+09:00", "2022-11-01T00:00:00+09:00", "2022-11-02T00:00:00+09:00"],
"areas"=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]},
{"area"=>{"name"=>"下北", "code"=>"020020"},
"weatherCodes"=>["111", "200", "200"],
"weathers"=>["晴れ 夕方 から くもり", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり"],
"winds"=>["東の風 後 南東の風", "南の風 海上 では 南の風 やや強く", "西の風"],
"waves"=>["1.5メートル 後 2メートル", "2メートル", "1.5メートル"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"},
"weatherCodes"=>["200", "200", "201"],
"weathers"=>["くもり 所により 昼前 雨", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり 時々 晴れ"],
"winds"=>["東の風 後 南の風", "南の風 後 南西の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["2メートル 後 2.5メートル うねり を伴う", "2.5メートル 後 2メートル うねり を伴う", "1.5メートル"]}]}
irb(main):049:0> b[0]['timeSeries'][0][1]
=> nil
irb(main):050:0> b[0]['timeSeries'][0]['areas']
=>
[{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]},
{"area"=>{"name"=>"下北", "code"=>"020020"},
"weatherCodes"=>["111", "200", "200"],
"weathers"=>["晴れ 夕方 から くもり", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり"],
"winds"=>["東の風 後 南東の風", "南の風 海上 では 南の風 やや強く", "西の風"],
"waves"=>["1.5メートル 後 2メートル", "2メートル", "1.5メートル"]},
{"area"=>{"name"=>"三八上北", "code"=>"020030"},
"weatherCodes"=>["200", "200", "201"],
"weathers"=>["くもり 所により 昼前 雨", "くもり 昼前 から 昼過ぎ 晴れ 所により 夜 雨", "くもり 時々 晴れ"],
"winds"=>["東の風 後 南の風", "南の風 後 南西の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["2メートル 後 2.5メートル うねり を伴う", "2.5メートル 後 2メートル うねり を伴う", "1.5メートル"]}]
irb(main):051:0> b[0]['timeSeries'][0]['areas'][0]
=>
{"area"=>{"name"=>"津軽", "code"=>"020010"},
"weatherCodes"=>["100", "112", "202"],
"weathers"=>["晴れ", "晴れ 後 くもり 夜遅く 雨 所により 夜 雷 を伴う", "くもり 一時 雨"],
"winds"=>["東の風", "南の風 海上 では 南東の風 やや強く", "西の風"],
"waves"=>["1メートル 後 1.5メートル", "1.5メートル", "1.5メートル"]}
irb(main):052:0> b[0]['timeSeries'][0]['areas'][0]['area']
=> {"name"=>"津軽", "code"=>"020010"}
irb(main):053:0> b[0]['timeSeries'][0]['areas'][0]['area'][0]
=> nil
irb(main):054:0> b[0]['timeSeries'][0]['areas'][0]['area']["name"]
=> "津軽"
なんとか抽出することができた。
ここまで来れた。
[]
は配列なので番号
{}
はハッシュなので文字列で検索する。
参考資料
ここからどう活かそうかな。
これから本題に入っていく。
これからも頑張ろう。