我设法整理了解析特定JSON的VBA代码:
Set S = CreateObject("ScriptControl")
S.Language = "JScript"
S.AddCode "function keys(O) { var k = new Array(); for (var x in O) { k.push(x); } return k; } "
Set JSON = S.Eval("(" & H.responsetext & ")")
Set JSON = CallByName(JSON, "responses", VbGet)
Set JSON = CallByName(JSON, "0", VbGet)
Set JSON = CallByName(JSON, "aggregations", VbGet)
Set JSON = CallByName(JSON, "2", VbGet)
Set JSON = CallByName(JSON, "buckets", VbGet)
Set Keys = S.Run("keys", JSON)
Dim jsonWS As String, jsonAA As String, jsonTS As String
For Each Key In Keys
Set TableRow = CallByName(JSON, Key, VbGet)
jsonWS = CallByName(TableRow, "key", VbGet)
Set TableRow = CallByName(TableRow, "3", VbGet)
Set TableRow = CallByName(TableRow, "buckets", VbGet)
Set TableRow = CallByName(TableRow, "Item 1", VbGet)
jsonAA = CallByName(TableRow, "key", VbGet)
Set TableRow = CallByName(TableRow, "1", VbGet)
jsonTS = CallByName(TableRow, "value_as_string", VbGet)
Next Key
然而我遇到了一个问题。当我运行此代码时,它会在引用“Item 1”Set TableRow = CallByName(TableRow, "Item 1", VbGet)
的行停止,并抛出以下错误:
“对象不支持此属性或方法”
我想在处理包含空格的名称时会出现问题。 我试过多个括号,引号,它没有用。我无法解决这个问题,因为我的javascript知识不存在。
JSON结构如下所示: