VBA post 方法请求正文 ("MSXML2.XMLHTTP"):解析错误 JSON:^ 需要“{”或“[”

VBA post method request body ("MSXML2.XMLHTTP"): Error Parsing JSON: ^ Expecting '{' or '['

我正在尝试通过以下查询 API 检索 JSON 响应对象。当我尝试读取 VBA 中的 responseText 时,我收到一个空结果。但是,完全相同的请求 returns 来自 PostMan 的正确数据。此外,来自发送不同请求主体的正确数据 returns。每当我尝试执行 Set Json = JsonConverter.ParseJson(strResponse) 时,我都会收到错误消息 Error Parsing JSON: ^ Expecting '{' or '['。你能帮忙吗?

这是VBA代码

Dim strUrl As String
Dim reqBody As String
        
    'For search GOSS service API-Step1
    strUrl = "https://gossrepo.ins.dell.com/gossv3/api/reporting/service/getrefid"
        
    'create a method for calling HTTP services
    Set hReq = CreateObject("MSXML2.XMLHTTP")
    
        With hReq
            .Open "POST", strUrl, blnAsync, False
             reqBody = "{""methodType"":extract,""sourceApplication"":DSA,""searchParameter"":[{""conditionType"":term,""key"":global_bu_id,""value"":11},{""conditionType"":wildcard,""key"":customer_num,""value"":[530007546697]},{""conditionType"":range,""key"":order_date,""value"":[{""from"":2021-08-31,""to"":2021-09-09}]},{""conditionType"":sort,""key"":order_date_time,""value"":desc}],""pageSize"":40,""pageNum"":0}"
            .SetRequestHeader "Content-type", "application/json"
            .Send reqBody
            While hReq.ReadyState <> 4
                DoEvents
            Wend
            'wrap the response in a JSON root tag "data" to count returned objects
            strResponse = hReq.ResponseText
            Debug.Print strResponse
        
        End With
    
    
    Set Json = JsonConverter.ParseJson(strResponse)

用不同的 post 主体更新了修复:

Dim strUrl As String
    Dim reqBody As String
        
    'For search GOSS service API-Step1
    strUrl = "https://gossrepo.us.dell.com/gossv3/api/reporting/service/getdata"
        
    'create a method for calling HTTP services
    Set hReq = CreateObject("MSXML2.XMLHTTP")
    
        With hReq
            .Open "POST", strUrl, blnAsync, False
             reqBody = "{""methodType"":""details"",""sourceApplication"":""DSA"",""pageNum"":0,""pageSize"":300,""searchParameter"":[{""conditionType"":""term"",""key"":""global_bu_id"",""value"":""11""},{""conditionType"":""wildcard"",""key"":""customer_num"",""value"":[""" & ws & """]},{""conditionType"":""range"",""key"":""order_date"",""value"":[{""from"":""" & ws11 & """,""to"":""" & ws12 & """}]},{""conditionType"":""sort"",""key"":""order_date_time"",""value"":""desc""}]}"
            .SetRequestHeader "Content-type", "application/json"
            .Send reqBody
            While hReq.ReadyState <> 4
                DoEvents
            Wend
            'wrap the response in a JSON root tag "data" to count returned objects
            strResponse = hReq.ResponseText
            
            
        End With
    
    
    Set Json = JsonConverter.ParseJson(strResponse)

可能您的请求是错误的,因此您没有得到预期的响应...查看返回的状态(hReq.statushReq.statusText),我敢打赌它是 400 Bad Request500 Internal Error 而不是 200 Ok。 (您也可以使用像 Fiddler 这样的检查代理来查看您在此处发送和接收的内容。)

我已经可以看到你的 request 正文无效 JSON 因为它里面有未加引号的字符串...它与你在 Postman 中显示的不完全一样!这就像问题(或其中一个问题)。你有例如"methodType": extract,但它必须是 "methodType": "extract"(在 VBA ""methodType"": ""extract"" 中)——你在 Postman 中做对了,但在你的代码中是错误的。

正如 CherryDT 所述 - 您的原始 reqBody 有很多遗漏的引号,而在您更新的 reqBody 中,您遗漏了 order_date 的引号并且您还引用了 pageSizepageNum 应该是数字的值,因此不需要引号:

下面应该为您提供与 Postman 中相同的 JSON 字符串:

reqBody = "{""methodType"":""extract"",""sourceApplication"":""DSA"",""searchParameter"":[{""conditionType"":""term"",""key"":""global_bu_id"",""value"":""11""},{""conditionType"":""wildcard"",""key"":""customer_num"",""value"":[""530007546697""]},{""conditionType"":""range"",""key"":""order_date"",""value"":[{""from"":""2021-08-31"",""to"":""2021-09-09""}]},{""conditionType"":""sort"",""key"":""order_date_time"",""value"":""desc""}],""pageSize"":40,""pageNum"":0}"

到目前为止,对我来说效果很好的一种方法是:

  1. 将 JSON 字符串从 Postman 复制到记事本
  2. 打开“替换”对话框 (Ctrl-H)
  3. 在查找内容中输入 "
  4. 在替换为
  5. 中输入""
  6. 单击全部替换

现在您可以将新字符串复制回您的 VBA 编辑器,它应该会产生与 Postman 相同的输出。