2016-06-08 77 views
0

在这里回答我自己的问题。
我已经做了JSON的一些工作在Excel VBA和大量的调查结果后,我将在Q中&这样做的一种格式 https://stackoverflow.com/help/self-answerhttp://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/在Windows的Excel VBA中,如何通过解析的JSON数组循环?

所以在计算器可以看到有关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数组。首先这里是一个miniscript方法 带帽子尖端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? Q1

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

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

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

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

回答

0

所以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