使用VB或VBA从Http请求获取JSON响应

时间:2018-06-14 19:50:55

标签: json vba http access-vba xmlhttprequest

我刚刚使用VBA发布了我的HTTP GET请求问题,我刚才得到了上一篇文章中给出的建议。我从“SetRequestHeader”中取出了“Basic”,我能够看到数据。由于我的数据是JSON格式,我必须正确地请求数据并将其存储到文本文件中。

在我的代码中,我设置P = JSON.parse(XMLHttpReq.responseText)是不定义的,我应该在这段代码中定义JSON ???由于这个问题,我无法运行它。

让我知道是否有人对此有任何不妥之处或其他建议!

我在下面添加了一些VBA代码来处理请求。

Sub Test()
    Dim sUrl As String, sAuth As String
    Dim P As Object
    Dim XMLHttpReq As MXXML2.ServerXMLHTTP

    sUrl = "https://api.ngs.nfl.com/tracking/game/play?gameKey=57444&playId=51"
    sAuth = "NGS AKIAIX2CQ7IEOKPOTKDQ:uNniaOp4jH8jcK9i/EtQhurlilc="

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", sUrl, False
        .setRequestHeader "Authorization", "Basic " & sAuth
        .send

        If XMLHttpReq.ReadyState = 4 Then
             If XMLHttpReq.Status = 200 Then

                  ' Process the JSON response here

                  Debug.Print "200 received"
                  Set P = JSON.parse(XMLHttpReq.responseText)
             Else
                  If XMLHttpReq.Status = 404 Then
                       ' Handle it
                  End If
             End If

        Debug.Print .getAllResponseHeaders
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

对于我得到的未经授权的响应,我使用了JSONConverter并将响应文本转换为JSON对象。对于我得到的回复,我将展示如何访问返回的消息。

注意:您需要将JSONConverter .bas添加到项目中,然后转到VBE>工具>引用并添加对Microsoft Scripting Runtime的引用。

Option Explicit   
Public Sub Test()
    Dim sUrl As String, sAuth As String

    Dim XMLHttpReq As MSXML2.ServerXMLHTTP60

    sUrl = "https://api.ngs.nfl.com/tracking/game/play?gameKey=57444&playId=51"
    sAuth = "NGS AKIAIX2CQ7IEOKPOTKDQ:uNniaOp4jH8jcK9i/EtQhurlilc="

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", sUrl, False
        .setRequestHeader "Authorization", sAuth
        .send

        Dim P As Object, key As Variant
        Set P = JsonConverter.ParseJson(.responseText)
        WriteTextFile .responseText
        For Each key In P.Keys
            Debug.Print key & " : " & P(key)
        Next key
    End With
End Sub

Public Sub WriteTextFile(ByVal htmlResponse As String)
    Dim fso As Object, f As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.CreateTextFile("C:\Users\User\Desktop\info.txt", True, True)
    f.Write htmlResponse
    f.Close
End Sub

修改

Option Explicit
Public Sub Test()
    Dim sUrl As String, sAuth As String, XMLHttpReq As MSXML2.ServerXMLHTTP60
    sUrl = "https://api.ngs.nfl.com/tracking/game/play?gameKey=57444&playId=51"
    sAuth = "NGS AKIAIX2CQ7IEOKPOTKDQ:uNniaOp4jH8jcK9i/EtQhurlilc="

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", sUrl, False
        .setRequestHeader "Authorization", sAuth
        .send
        WriteTextFile .responseText, "C:\Users\User\Desktop\info.txt"
    End With
End Sub

Public Sub WriteTextFile(ByVal htmlResponse As String, ByVal fileName As String)
    Dim fso As Object, f As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.CreateTextFile(fileName, True, True)
    f.Write htmlResponse
    f.Close
End Sub