VBA webResponse Response.data 将类型从 "Dictionary" 更改为 "collection" 时出错

VBA webResponse Response.data getting error when changing type from "Dictionary" to "collection"

这是问题所在:我有以下代码

Dim Client As New WebClient
Dim Request As New WebRequest
Dim Response As WebResponse
    
Dim responseData As Scripting.Dictionary
    
Request.UserAgent = VBA.Environ("USERNAME")
Request.AddHeader "AuthToken", m_token
Request.AddHeader "APIKey", m_key
Request.Method = WebMethod.HttpPost
    
Dim Auth As New HttpBasicAuthenticator
Auth.Setup "username", "Password"
Set Client.Authenticator = Auth
    
Dim Body As New Dictionary
If condition1 Then
        Body.Add "status", "open"
Else
        Body.Add "status", "closed"
End If
    
Client.BaseUrl = server_api & "Tasks/" & m_id
    
Set Request.Body = Body
Set Response = Client.Execute(Request)
    
If (Response.StatusCode = Ok) Then
        Debug.Print TypeName(Response.Data) 'returns Dictionary
        Set responseData = Response.Data '.Item(1)
        Debug.Print "ID: " & responseData("id")
        Debug.Print "message: " & responseData("message")
End If

请求成功执行,程序没有错误,Response.data 的类型名称为 Dictionary,但消息是:

'statut' doesn't exist in table

所以我尝试通过将“状态”名称更改为所需的“tableColunm”来解决此问题,该名称在数据库中具有 int32 类型。

应用的更改在以下代码中,结果在注释中:

    If condition1 Then
        Body.Add "tableColunm", 1 'I tried also with "1"
    Else
        Body.Add "tableColunm", 2 ' I tried also with "2"
    End If        Client.BaseUrl = server_api & "Tasks/" & m_id
    
    Set Request.Body = Body
    Set Response = Client.Execute(Request)
    
    If (Response.StatusCode = Ok) Then
        Debug.Print TypeName(Response.Data) 'returns Collection
        Set responseData = Response.Data '.Item(1) ' error here, and the program crashed
        Debug.Print "ID: " & responseData("id")
        Debug.Print "message: " & responseData("message")
    End If

现在Response.data的typename是collection,我想这就是程序崩溃的原因

有什么帮助吗?谢谢

最后,加上这个

For Each elt In Response.Data
    Debug.Print TypeName(elt)
Next

循环只执行一次,输出Dictionary。这意味着在第二种情况下,字典被封装在一个集合中

所以我使用 elt 解决了我的问题