2016-06-08 99 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