我写了以下VBA宏
Sub getMatches()
Dim strURL As String
Dim strJSON As String
Dim strCompetition As Integer
Dim strSeason As Integer
Dim strMatchDay As Integer
Dim i As Integer
'strURL = "https://api.football-data.org/v2/competitions/2021/matches?matchday=1&season=2019"
strURL = "https://api.football-data.org/v2/competitions/" & strSeason & "/matches?matchday=" & strMatchDay & "&season=" & strSeason
strCompetition = Range("B2").Value
strSeason = Range("B4").Value
strMatchDay = Range("B6").Value
MsgBox strCompetition
MsgBox strSeason
MsgBox strMatchDay
MsgBox strURL
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "Get", strURL
MyRequest.setRequestHeader "X-Auth-Token", "personal_code"
MyRequest.Send
Dim Json As Object
Set Json = JsonConverter.ParseJson(MyRequest.ResponseText)
MsgBox MyRequest.ResponseText
运行此脚本,将获得以下输出:
MsgBox strCompetition
MsgBox strSeason
MsgBox strMatchDay
MsgBox strURL
为什么URL不包含我之前声明的变量? 我希望URL定义如下:“ https://api.football-data.org/v2/competitions/2021/matches?matchday=1&season=2019”
答案 0 :(得分:0)
您要在将变量值放入URL之后设置它们的值。在分配了所有其他变量(同上为matchday变量的同上)之后,只需将URL创建行移至即可。
strCompetition = Range("B2").Value
strSeason = Range("B4").Value
strMatchDay = Range("B6").Value
matchday=" & strMatchDay & "&season=" & strSeason
strURL = "https://api.football-data.org/v2/competitions/" & strSeason & "/matches?matchday=" & strMatchDay & "&season=" & strSeason