Parsing JSON in Excel VBA

后端 未结 11 1004
长发绾君心
长发绾君心 2020-11-22 09:58

I have the same issue as in Excel VBA: Parsed JSON Object Loop but cannot find any solution. My JSON has nested objects so suggested solution like VBJSON and vba-json do not

11条回答
  •  不要未来只要你来
    2020-11-22 10:26

    Simpler way you can go array.myitem(0) in VB code

    my full answer here parse and stringify (serialize)

    Use the 'this' object in js

    ScriptEngine.AddCode "Object.prototype.myitem=function( i ) { return this[i] } ; "
    

    Then you can go array.myitem(0)

    Private ScriptEngine As ScriptControl
    
    Public Sub InitScriptEngine()
        Set ScriptEngine = New ScriptControl
        ScriptEngine.Language = "JScript"
        ScriptEngine.AddCode "Object.prototype.myitem=function( i ) { return this[i] } ; "
        Set foo = ScriptEngine.Eval("(" + "[ 1234, 2345 ]" + ")") ' JSON array
        Debug.Print foo.myitem(1) ' method case sensitive!
        Set foo = ScriptEngine.Eval("(" + "{ ""key1"":23 , ""key2"":2345 }" + ")") ' JSON key value
        Debug.Print foo.myitem("key1") ' WTF
    
    End Sub
    

提交回复
热议问题