在VBA中访问Variant / Object / JScriptTypeInfo属性

时间:2017-09-28 12:52:19

标签: json excel vba excel-vba jscript

我有以下代码VBA代码(对于Excel模块)

Function getDesc(ByVal pCode As String) As String
    Dim oRequest As Object
    Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    oRequest.Open "GET", "https://my.url.com/?filter=CODE=" & pCode, False
    oRequest.SetRequestHeader "Accept", "application/json"
    oRequest.Send ""

    Set props = jsonDecode(oRequest.ResponseText)

    getDesc = props.row_data
End Function

Function jsonDecode(jsonString As Variant)
    Set sc = CreateObject("ScriptControl"): sc.Language = "JScript"
    Set jsonDecode = sc.Eval("(" + jsonString + ")")
End Function

props.row_data的结果如图所示 enter image description here

我无法弄清楚如何将属性LONG_DESCRIPTION分配给getDesc =

我需要什么样的语法?或者,我将接受任何允许我返回LONG_DESCRIPTION字符串的解决方案。

1 个答案:

答案 0 :(得分:0)

您不需要外部库来实现这一点:

Option Explicit

Function getDesc(ByVal pCode As String) As String
    Dim oRequest As Object
    Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    oRequest.Open "GET", "https://my.url.com/?filter=CODE=" & pCode, False
    oRequest.SetRequestHeader "Accept", "application/json"
    oRequest.Send ""

    Set props = jsonDecode(oRequest.ResponseText)

    Set sc = CreateObject("ScriptControl"): sc.Language = "JScript"
    sc.AddCode "function jsGetDesc(obj) { return obj[0]['LONG_DESCRIPTION']; }"
    getDesc = sc.Run("jsGetDesc", props.row_data)
End Function