使用VBA-WEB调用FedEx API时JSON解析报错求助
ERROR - WebHelpers.ParseByFormat: 11000, An error occurred during parsing
10001: Error parsing JSON:
?
ERROR - WebHelpers.ParseByFormat: 11000, An error occurred during parsing
10001: Error parsing JSON:
?
? Ñj?0?ïûÁë*:mw'ê¨ÆLÝ?!irZ??¸$?Ié»OÓÎÝm;Wÿ?ÿ|?óãE?¦Ì´RdÜy@ND²Ç¦ziª-??ú3Kî?¿¡îü?òãÒûUè?þ~µæï3¾v?×6PJ*=Ö¼Z?æ<'»Á$?éLµÍIK/.pÅõ«ã(O?2.ráÝ^?>ge?ÜÌ5hM¶)?]OÅ?iH*Ô?ÒRXj5Rðqjp?w ЩçÔ ¢?#£D´?3?_??ÿ?zN¾ý-§9©w¿?«?ÄÍ?IaÆÏ_yt?®7ÿEmz[\¾ _.È
我尝试用VBA-WEB库向FedEx API发送JSON请求时,出现上述解析错误。API明确要求接收JSON格式请求,我用多个在线工具校验过自己的JSON,格式是有效的,所以问题应该出在其他环节。
我已经参考同类问题的解决方案,试过设置Request.Format = WebFormat.JSON和Request.ContentType = "application/JSON"两种方式,但返回的错误完全一样。
下面附上我的代码和请求体,麻烦帮忙排查问题,需要补充信息的话随时告知。
代码
Public Sub API() ' 创建客户端 Dim Client As New WebClient Client.BaseUrl = "https://apis-sandbox.fedex.com" ' 创建请求 Dim Request As New WebRequest ' 设置请求头 Request.Resource = "/ship/v1/shipments" Request.Method = WebMethod.HttpPost Request.ContentType = "application/json" Request.Accept = "application/json" Request.AddHeader "Authorization", "Bearer XXX" ' 请求体结构 Dim Body as New Scripting.Dictionary ' 请求体内容见下方JSON Set Request.Body = Body ' 获取响应 Dim Response As WebResponse Set Response = Client.Execute(Request) ' 输出响应内容 Debug.Print Response.Content ' 打印完整响应 End Sub
请求体
{ "requestedShipment": { "shipper": { "address": { "streetLines": [ "12 Sesame Street" ], "city": "Toronto", "countryCode": "CA" }, "contact": { "phoneNumber": "1112223333" } }, "recipients": [ { "address": { "streetLines": [ "123 10th Street" ], "city": "San Francisco", "countryCode": "US" }, "contact": { "phoneNumber": "3334445555" } } ], "pickupType": "CONTACT_FEDEX_TO_SCHEDULE", "serviceType": "INTERNATIONAL_FIRST", "packagingType": "FEDEX_BOX", "shippingChargesPayment": { "paymentType": "SENDER" }, "labelSpecification": { "labelStockType": "PAPER_4x6", "imageType": "PDF" }, "requestedPackageLineItems": [ { "weight": { "units": "KG", "value": 22.22 } } ] }, "labelResponseOptions": "URL_ONLY", "accountNumber": { "value": "123456789" } }
内容的提问来源于stack exchange,提问作者Kyle Atkinson

