In Excel VBA on Windows, how to loop through a JSON array parsed?

前端 未结 1 1342
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 04:23

answering my own question here.
I have done some work with JSON in Excel VBA and lots of findings to post which I will do so in Q & A format https://stackoverflow.co

相关标签:
1条回答
  • 2020-12-04 05:01

    So VBA.CallByName also accesses elements in an array as well as find the array's length

    '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
    
    0 讨论(0)
提交回复
热议问题