我有与Excel VBA相同的问题:Parsed JSON Object Loop但无法找到任何解决方案.我的JSON有嵌套对象所以像VBJSON和vba-json这样的建议解决方案对我不起作用.我还修复了其中一个正常工作,但结果是一个调用堆栈溢出,因为doProcess函数的许多递归.
最好的解决方案似乎是原始帖子中看到的jsonDecode函数.它非常快速且高效; 我的对象结构是JScriptTypeInfo类型的通用VBA对象.
此时的问题是我无法确定对象的结构是什么,因此,我事先并不知道将存在于每个通用对象中的键.我需要遍历通用VBA对象来获取键/属性.
如果我的解析javascript函数可以触发VBA函数或sub,那将是非常好的.
下面的示例...从解析的JSON字符串循环对象返回错误"对象不支持此属性或方法".任何人都可以建议如何使这项工作?非常感谢(我在这里问了6个小时寻找答案).
用于将JSON字符串解析为对象的函数(这可以正常工作).
Function jsonDecode(jsonString As Variant)
Set sc = CreateObject("ScriptControl"): sc.Language = "JScript"
Set jsonDecode = sc.Eval("(" + jsonString + ")")
End Function
Run Code Online (Sandbox Code Playgroud)
循环解析对象将返回错误"对象不支持此属性或方法".
Sub TestJsonParsing()
Dim arr As Object 'Parse the json array into here
Dim jsonString As String
'This works fine
jsonString = "{'key1':'value1','key2':'value2'}"
Set arr = jsonDecode(jsonString)
MsgBox arr.key1 'Works (as long as I know the key name)
'But this loop doesn't work - what am I doing wrong?
For Each keyName In arr.keys 'Excel errors out …
Run Code Online (Sandbox Code Playgroud) 我需要处理一个JSON对象,它是Excel VBA中XMLHTTPRequest的响应.我写了下面的代码,但它不起作用:
Dim sc As Object
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
Dim strURL As String: strURL = "blah blah"
Dim strRequest
Dim XMLhttp: Set XMLhttp = CreateObject("msxml2.xmlhttp")
Dim response As String
XMLhttp.Open "POST", strURL, False
XMLhttp.setrequestheader "Content-Type", "application/x-www-form-urlencoded"
XMLhttp.send strRequest
response = XMLhttp.responseText
sc.Eval ("JSON.parse('" + response + "')")
Run Code Online (Sandbox Code Playgroud)
我收到错误运行时错误'429'ActiveX组件无法在行中 创建对象Set sc = CreateObject("ScriptControl")
解析JSON对象后,如何访问JSON对象的值?
PS My JSON对象示例: {"Success":true,"Message":"Blah blah"}