我有以下代码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
我无法弄清楚如何将属性LONG_DESCRIPTION
分配给getDesc =
我需要什么样的语法?或者,我将接受任何允许我返回LONG_DESCRIPTION
字符串的解决方案。
答案 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