excel中宏解析java后台返回的json串

Private Sub CommandButton1_Click()
    Dim scobj As Object
    Dim status As Integer
    Set scobj = CreateObjectx86("MSScriptControl.ScriptControl")
    scobj.Language = "JavaScript"
    scobj.AddCode ("var res = " & result)
    'result为java后台返回的json串

    'JSON对象获取属性的表示方法:对象.属性
    '属性的值如果是个包含多个对象的数组,可以使用索引表示取得对象:对象.属性[0]
    'Eval是表达式求值
    status = scobj.Eval("res.status")
    score = scobj.Eval("res.data.score")
    
    'MsgBox status
    '在代码结束之前接的关闭对象
    'CreateObjectx86 , True ' close mshta host window at the end

    
    If status = 404 Then
        MsgBox "404报错,循环退出"
        Exit For
    Else
        ActiveSheet.Cells(i, 9) = status
        ActiveSheet.Cells(i, 10) = score
    End If
    t = sleep1()
End Sub
Public Function CreateObjectx86(Optional sProgID, Optional bClose = False) '通用函数,在32位和64位office都能正确创建脚本对象
 
    Static oWnd As Object
    Dim bRunning As Boolean
 

    bRunning = InStr(TypeName(oWnd), "HTMLWindow") > 0
    If bClose Then
        If bRunning Then oWnd.Close
        Exit Function
    End If
    If Not bRunning Then
        Set oWnd = CreateWindow()
        oWnd.execScript "Function CreateObjectx86(sProgID): Set CreateObjectx86 = CreateObject(sProgID): End Function", "VBScript"
    End If
    Set CreateObjectx86 = oWnd.CreateObjectx86(sProgID)

    Set CreateObjectx86 = CreateObject(sProgID)

 
End Function
 
Public Function CreateWindow()
 
    Dim sSignature, oShellWnd, oProc
 
    On Error Resume Next
    sSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38)
    CreateObject("WScript.Shell").Run "%systemroot%\syswow64\mshta.exe about:""about:""", 0, False
    Do
        For Each oShellWnd In CreateObject("Shell.Application").Windows
            Set CreateWindow = oShellWnd.GetProperty(sSignature)
            If Err.Number = 0 Then Exit Function
            Err.Clear
        Next
    Loop
 
End Function

你可能感兴趣的:(excel中宏解析java后台返回的json串)