【问题标题】:In Excel VBA on Windows, how to loop through a JSON array parsed?在 Windows 上的 Excel VBA 中,如何遍历解析的 JSON 数组?
【发布时间】:2016-10-09 05:24:41
【问题描述】:

在这里回答我自己的问题。
我在 Excel VBA 中使用 JSON 完成了一些工作,并发布了许多发现,我将以问答格式进行发布 https://stackoverflow.com/help/self-answerhttps://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/

所以在 stackoverflow 的其他地方可以看到有关在 VBA 中解析 JSON 的问题,但他们似乎错过了一两个技巧。

首先,我放弃使用自定义 JSON 解析库,而是使用 ScriptControl 的 Eval 方法作为我所有 JSON 代码的基础。 我们还表达了对原生 Microsoft 解决方案的偏好。

这是一个先前的问题In Excel VBA on Windows, how to mitigate issue of dot syntax traversal of parsed JSON broken by IDE's capitalisation behaviour?,此问题建立在此基础之上。它展示了如何使用 VBA.CallByName 更加健壮 而不是使用点语法来遍历解析的 JSON 对象。

在这个问题中,询问如何遍历已解析的 JSON 数组。首先这里是一个小脚本方法 给 ozmike 戴帽子https://stackoverflow.com/users/334106/ozmike

'Tools->References->
'Microsoft Script Control 1.0;  {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}; C:\Windows\SysWOW64\msscript.ocx

Private Sub TestJSONParsingArrayWithMiniScript()
    'Hat tip to ozmike  https://stackoverflow.com/users/334106/ozmike
    'Based on https://stackoverflow.com/questions/5773683/excel-vba-parsed-json-object-loop#19359035
    Dim oScriptEngine As ScriptControl
    Set oScriptEngine = New ScriptControl
    oScriptEngine.Language = "JScript"
    oScriptEngine.AddCode "Object.prototype.myitem=function( i ) { return this[i] } ; "
    
    Dim sJsonString As String
    sJsonString = "[ 1234, 2345 ]"
    
    Dim objJSON As Object
    Set objJSON = oScriptEngine.Eval("(" + sJsonString + ")")
    
    Debug.Assert objJSON.myitem(0) = 1234
    Debug.Assert objJSON.myitem(1) = 2345
    

End Sub

不管你信不信,VBA.CallByName 不仅可以用于获取数组的长度,还可以用于访问元素。查看答案。

这是系列 5 的问题 2。这是完整系列

第一季度In Excel VBA on Windows, how to mitigate issue of dot syntax traversal of parsed JSON broken by IDE's capitalisation behaviour?

第二季度In Excel VBA on Windows, how to loop through a JSON array parsed?

第三季度In Excel VBA on Windows, how to get stringified JSON respresentation instead of “[object Object]” for parsed JSON variables?

第四季度In Windows Excel VBA,how to get JSON keys to pre-empt “Run-time error '438': Object doesn't support this property or method”?

Q5In Excel VBA on Windows, for parsed JSON variables what is this JScriptTypeInfo anyway?

【问题讨论】:

    标签: json excel vba


    【解决方案1】:

    所以 VBA.CallByName 也可以访问数组中的元素并找到数组的长度

    'Tools->References->
    'Microsoft Script Control 1.0;  {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}; C:\Windows\SysWOW64\msscript.ocx
    
    Private Sub TestJSONParsingArrayWithCallByName()
    
        Dim oScriptEngine As ScriptControl
        Set oScriptEngine = New ScriptControl
        oScriptEngine.Language = "JScript"
    
        Dim sJsonString As String
        sJsonString = "[ 1234, 2345 ]"
    
        Dim objJSON As Object
        Set objJSON = oScriptEngine.Eval("(" + sJsonString + ")")
    
        '* Using VBA.CallByName we get the length of the array
    
        Dim lLength As Long
        lLength = VBA.CallByName(objJSON, "length", VbGet)
    
        Debug.Assert lLength = 2
    
        '* Believe or not one uses "0","1",.... with callbyname to get an element
        Debug.Assert VBA.CallByName(objJSON, "0", VbGet) = 1234
        Debug.Assert VBA.CallByName(objJSON, "1", VbGet) = 2345
    
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2014-04-03
      • 1970-01-01
      相关资源
      最近更新 更多