我試圖通過下面的查詢API來檢索一個JSON回應物件。當我試圖在VBA中讀取回應文本時,我收到一個空的結果。然而,完全相同的請求從PostMan回傳正確的資料。同時,發送不同的請求體也會回傳正確的資料。每當我試圖執行Set Json = JsonConverter.ParseJson(strResponse)時,我就會收到錯誤資訊Error Parsing JSON: ^ Expecting '{' or '['. 你能幫助我嗎?
Dim strUrl As String>
Dim reqBody As String
'用于搜索GOSS服務的API-Step1。
strUrl = "https://gossrepo.ins.dell.com/gossv3/api/reporting/service/getrefid"/span>
'創建一個用于呼叫HTTP服務的方法。
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"/span>, "application/json"/span>.
.發送reqBody
While hReq.ReadyState <> 4 。
觸發事件(DoEvents)
終止
'將回應包在JSON根標簽 "data "中以計算回傳的物件。
strResponse = hReq.Respontext
Debug.Print strResponse
結束 與
Set Json = JsonConverter.ParseJson(strResponse)
用不同的帖子主體更新了固定的內容:
Dim strUrl As String>
Dim reqBody As String
'用于搜索GOSS服務的API-Step1。
strUrl = "https://gossrepo.us.dell.com/gossv3/api/reporting/service/getdata"/span>
'創建一個用于呼叫HTTP服務的方法。
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"/span>, "application/json"/span>.
.發送reqBody
While hReq.ReadyState <> 4 。
觸發事件(DoEvents)
終止
'將回應包在JSON根標簽 "data "中以計算回傳的物件。
strResponse = hReq.Respontext
結束 與
Set Json = JsonConverter.ParseJson(strResponse)
uj5u.com熱心網友回復:
很可能你的請求是錯誤的,你沒有得到預期的回應,因為它。看看回傳的狀態(hReq.status和hReq.statusText),我打賭它是400 Bad Request或500 Internal Error而不是200 Ok。(你也可以使用像Fiddler這樣的檢查代理來看看你在這里到底發送和接收了什么。)
我已經看到了你的請求。
我已經可以看到你的request體是無效的JSON,因為它有未參考的字串...... 這和你在Postman中顯示的情況不完全一樣! 這就是問題所在(或問題之一)。你有例如"methodType": extract,但它必須是"methodType": "extract"(在VBA中""methodType"": ""extract"")--你在Postman中做得很正確,但在你的代碼中卻錯了。
uj5u.com熱心網友回復:
正如CherryDT所提到的--你原來的reqBody有很多缺失的引號,在你更新的reqBody中,你缺失了order_date的引號,而且你還參考了pageSize和pageNum的值,而這應該是一個數字,因此不需要引號。
下面應該給出與你在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}"
到目前為止,對我來說效果很好的一個方法是:
- 從Postman復制JSON字串到記事本 打開替換對話框(Ctrl-H)
- 在Find What中輸入
"。
- 在Replace with中輸入
""。
- 點擊全部替換 。
現在你可以把新的字串復制到你的VBA編輯器中,它應該產生與Postman相同的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/320464.html
標籤:
