0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBA-JSONで天気予報(明後日まで・週間)JSONファイルを読んでみる

Last updated at Posted at 2022-07-30

目的

VBA-tools/VBA-JSONを使用してPythonで天気予報(明後日まで・週間)JSONファイルを読んでみる
を参考にJSONファイルを読んでみる

追加モジュール

VBA-JSON v2.3.1よりVBA-JSON-2.3.1.zipをDL&解凍する
JsonConverter.basをVBAプロジェクトに追加する

vbaのサンプルコード

以下のサンプルのコメントは【Colab / Python】気象庁API - 気象データの収集の記述を参考にしています
※コピペともいう・・・・

Dim item    As Variant
Dim itm     As Variant
Dim json    As Object
Dim url     As String
Dim ret     As String
Dim idx     As Integer

'
' 天気概況(明後日まで)
'
url = "https://www.jma.go.jp/bosai/forecast/data/overview_week/130000.json"
ret = WorksheetFunction.WebService(url)
'
Set json = JsonConverter.ParseJson(ret)
Debug.Print ("発表者:")
Debug.Print " " + json("publishingOffice")
Debug.Print ("報告日時:")
Debug.Print " " + json("reportDatetime")
Debug.Print ("対象地域:")
Debug.Print " " + json("headTitle")
Debug.Print ("天気概況(週間):")
Debug.Print " " + json("text")
'
'   直近3日間+週間
'
url = "https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json"
ret = WorksheetFunction.WebService(url)
'
Set json = JsonConverter.ParseJson(ret)
'
'   直近3日間(当日含む)の天気予報
'
Debug.Print ("発表者:")
Debug.Print " " + json(1)("publishingOffice")
Debug.Print ("報告日時:")
Debug.Print " " + json(1)("reportDatetime")
'
'   直近3日間(当日含む)の天気予報
'
Debug.Print " "
Debug.Print ("日時:")
Debug.Print " " + json(1)("timeSeries")(1)("timeDefines")(1)
Debug.Print " " + json(1)("timeSeries")(1)("timeDefines")(2)
Debug.Print " " + json(1)("timeSeries")(1)("timeDefines")(3)
'
Debug.Print " "
For Each item In json(1)("timeSeries")(1)("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
'
    idx = 0
    For Each itm In json(1)("timeSeries")(1)("timeDefines")
        idx = idx + 1
        Debug.Print " " + json(1)("timeSeries")(1)("timeDefines")(idx)
        Debug.Print " " + item("weatherCodes")(idx)
        Debug.Print " " + item("weathers")(idx)
        Debug.Print " " + item("winds")(idx)
        Debug.Print " " + item("waves")(idx)
    Next
    Debug.Print " "
Next
'
'   直近6時間毎の降水確率
'
Debug.Print " "
For Each item In json(1)("timeSeries")(2)("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
    idx = 0
    For Each itm In json(1)("timeSeries")(2)("timeDefines")
        idx = idx + 1
        Debug.Print json(1)("timeSeries")(2)("timeDefines")(idx) + " : " + item("pops")(idx)
    Next
    Debug.Print " "
Next
'
'   翌日の最低気温と日中の最高気温
'
Debug.Print " "
For Each item In json(1)("timeSeries")(3)("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
    idx = 0
    For Each itm In json(1)("timeSeries")(3)("timeDefines")
        idx = idx + 1
        Debug.Print json(1)("timeSeries")(3)("timeDefines")(idx) + " : " + item("temps")(idx)
    Next
    Debug.Print " "
Next
'
'   週間予報 (天気, 降水確率, 予報に対する信頼度)
'
Debug.Print " "
For Each item In json(2)("timeSeries")(1)("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
    idx = 0
    For Each itm In json(2)("timeSeries")(1)("timeDefines")
        idx = idx + 1
        Debug.Print " " + item("weatherCodes")(idx) _
                + " : " + item("pops")(idx) _
                + " : " + item("reliabilities")(idx)
    Next
    Debug.Print " "
Next
'
'   週間予報 (最低気温, 最低気温の予測上限, 最低気温の予測下限,
'   日中の最高気温, 日中の最高気温の予測上限, 日中の最高気温の予測下限)
'
Debug.Print " "
For Each item In json(2)("timeSeries")(2)("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
    idx = 0
    For Each itm In json(2)("timeSeries")(2)("timeDefines")
        idx = idx + 1
        Debug.Print " " + json(2)("timeSeries")(2)("timeDefines")(idx)
        Debug.Print " " + item("tempsMin")(idx) _
                  + " : " + item("tempsMinLower")(idx) _
                  + " : " + item("tempsMax")(idx) _
                  + " : " + item("tempsMaxUpper")(idx) _
                  + " : " + item("tempsMaxLower")(idx)
    Next
    Debug.Print " "
Next
'
'   向こう一週間(明日から7日先まで)の平年値 (最低気温, 最高気温)
'
Debug.Print " "
For Each item In json(2)("tempAverage")("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
    Debug.Print item("min") + " : " + item("max")
    idx = 0
    Debug.Print " "
Next
'
'   向こう一週間(明日から7日先まで)の平年値 (降水量の7日間合計)
'
Debug.Print " "
For Each item In json(2)("precipAverage")("areas")
    Debug.Print item("area")("code") + " : " + item("area")("name")
    Debug.Print item("min") + " : " + item("max")
    idx = 0
    Debug.Print " "
Next

参考にしたのは以下のサイト

VBA-JSON v2.3.1
【Colab / Python】気象庁API - 気象データの収集
宇宙一わかりやすい?VBA-JSONを使ったJSONパースのしかた
Pythonで天気予報(明後日まで・週間)JSONファイルを読んでみる
C#で天気予報JSONファイルを読んでみる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?