相关疑难解决方法(0)

Excel VBA:解析的JSON对象循环

下面的示例...从解析的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)

excel vba json scriptcontrol

21
推荐指数
2
解决办法
5万
查看次数

标签 统计

excel ×1

json ×1

scriptcontrol ×1

vba ×1