エクセルのvbaでjsonファイルを読み込んでシートに記載する
準備としてVBA-JSON(JsonConverter.bas)をインポートしておく
https://github.com/VBA-tools/VBA-JSON
Visual Basic Editor の参照設定でMicrosoft Scripting Runtimeにチェックを入れる
jsonファイル(test.json)
{"aa":[
{
"id":"1",
"contents":"あああaa"
},
{
"id":"2",
"contents":"hogehoge"
}
]}
vbaのマクロ
Set jsonObj = JsonConverter.ParseJson(buf)とすることで、オブジェクトに代入できて
.countで要素数を返したり
()で配列みたいに指定したり
For Each Key でキー名を順に取り出したり出来るみたいです。
キー名は""で囲うのは忘れずに
Sub open_json()
Dim buf, jsonObj, cnt, i
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.LoadFromFile "./test.json"
buf = .ReadText
.Close
End With
Set jsonObj = JsonConverter.ParseJson(buf)
For i = 1 To jsonObj("aa").Count
cnt = 1
For Each Key In jsonObj("aa")(1)
Cells(1, cnt) = Key
If IsObject(jsonObj("aa")(i)(Key)) = False Then Cells(i + 1, cnt) = jsonObj("aa")(i)(Key)
cnt = cnt + 1
Next
Next i
End Sub