Json 在VBA中访问Variant/Object/JScript类型信息属性

Json 在VBA中访问Variant/Object/JScript类型信息属性,json,vba,excel,jscript,Json,Vba,Excel,Jscript,我有以下代码VBA代码(用于Excel模块) props.row_数据的结果如图所示 我不知道如何将属性LONG\u DESCRIPTION分配给getDesc= 我需要的正确语法是什么?或者,我将接受任何允许我返回LONG\u DESCRIPTION字符串的解决方案 试试下面的代码将模块导入VBA项目以进行JSON处理。 选项显式 函数getDesc(ByVal pCode作为字符串)作为字符串 作为对象的模糊搜索 将字符串作为字符串 Dim vJSON作为变量 将状态设置为字符串 设置oR

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

props.row_数据的结果如图所示

我不知道如何将属性
LONG\u DESCRIPTION
分配给
getDesc=


我需要的正确语法是什么?或者,我将接受任何允许我返回
LONG\u DESCRIPTION
字符串的解决方案

试试下面的代码将模块导入VBA项目以进行JSON处理。

选项显式
函数getDesc(ByVal pCode作为字符串)作为字符串
作为对象的模糊搜索
将字符串作为字符串
Dim vJSON作为变量
将状态设置为字符串
设置oRequest=CreateObject(“WinHttp.WinHttpRequest.5.1”)
oRequest.打开“获取”https://my.url.com/?filter=CODE=&pCode,False
oRequest.SetRequestHeader“接受”、“应用程序/json”
oRequest,发送
sJSONString=oRequest.ResponseText
解析sJSONString、vJSON、sState
getDesc=vJSON(“行数据”)(0)(“长描述”)
端函数

使用JSON字符串测试
{'row_data':[{'LONG_DESCRIPTION':'desc_string'}]}

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

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

请发布
oRequest.ResponseText
内容,至少是相关部分。请注意,以这种方式使用ScriptControl ActiveX可能会使系统容易受到响应中恶意JS代码的攻击。查看和。使用此库可能会有所帮助
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