VBA-Excel; JSON请求由于单元格中的0而不是值而产生错误

时间:2018-11-06 19:13:48

标签: json excel vba

我写了以下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

enter image description here

MsgBox strSeason

enter image description here

MsgBox strMatchDay

enter image description here

MsgBox strURL

enter image description here

为什么URL不包含我之前声明的变量? 我希望URL定义如下:https://api.football-data.org/v2/competitions/2021/matches?matchday=1&season=2019

1 个答案:

答案 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