VBA/JSON解析数组中的JSON数据
我正在尝试解析来自一个包含多个数组的json对象的数据。 以下代码作为示例:VBA/JSON解析数组中的JSON数据,json,vba,Json,Vba,我正在尝试解析来自一个包含多个数组的json对象的数据。 以下代码作为示例: { "Version": "1.0", "I.1": [ { "MethodenID": "I.1.3", "Ranking": "1", "Punktzahl": "20" }, { "MethodenID": "I.1.1", "Ranking": "3", "Punktzahl": "68" }
{
"Version": "1.0",
"I.1": [
{
"MethodenID": "I.1.3",
"Ranking": "1",
"Punktzahl": "20"
},
{
"MethodenID": "I.1.1",
"Ranking": "3",
"Punktzahl": "68"
}
],
"I.2": [
{
"MethodenID": "I.2.2",
"Ranking": "1",
"Punktzahl": "87"
},
{
"MethodenID": "I.2.1",
"Ranking": "2",
"Punktzahl": "67"
}
]}
我只想从内部数组中获取对象。
我尝试了power查询工具excel,但在单独访问每个内部列表时遇到了一些问题
在那之后,我尝试了我的运气与vba,但有同样的问题
我现在的问题是:有可能用vba实现这一点吗
如果没有,是否可以将文件转换为更可读的状态
我已经试过JsonConverter了
Private Sub CommandButton1_Click()
Dim fd As Office.FileDialog
Dim ws As Worksheet
Set ws = Worksheets("Tabelle1")
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select a json file"
.AllowMultiSelect = False
If .Show() Then
ws.Cells(1, 200) = "1"
Filename = (.SelectedItems(1))
Dim content As String
Dim iFile As Integer: iFile = FreeFile
Open Filename For Input As #iFile
content = Input(LOF(iFile), iFile)
'MsgBox (content)
'Parse JSON String
Dim products As Object
Set products = JsonConverter.ParseJson(content)
i = 1
For Each Product In products
ws.Cells(i, 1) = Product("MethodenID")
ws.Cells(i, 2) = Product("Ranking")
ws.Cells(i, 3) = Product("Punktzahl")
i = i + 1
Next
Close #iFile
End If
End With
End Sub
我从本教程中得出以下结论:
遗憾的是,我似乎无法获得内部数组中的值。类似这样的内容:
Dim k, arr
Dim Json As Object
'reading json from a worksheet cell...
Set Json = JsonConverter.ParseJson(Range("C3").Value)
For Each k In Array("I.1", "I.2")
Set arr = Json(k) 'arr here is a VBA Collection object
For Each o In arr 'o here is a scripting dictionary
Debug.Print o("MethodenID"), o("Ranking"), o("Punktzahl")
Next o
Next k
您可以使用VBA JSON解析器模块:忘记一些有关转换器的重要信息。编辑的问题
products
是一个脚本字典,带有键“I.1”和“I.2”,每个键将返回一个包含两个项的集合,这两个项依次是表示要访问的对象的字典对象。