在 excel vba 中将 JSON 发布到网络 [英] Post JSON to web in excel vba

查看:18
本文介绍了在 excel vba 中将 JSON 发布到网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一些 VBA 发布一些 JSON:

I want to POST some JSON with some VBA:

Dim sURL As String, sHTML As String, sAllPosts As String
Dim oHttp As Object
Dim blWSExists As Boolean
Set oHttp = CreateObject("MSXML2.XMLHTTP")
sURL = "some webste.com"
oHttp.Open "POST", sURL, False
oHttp.setRequestHeader "Content-type", "application/json"
oHttp.setRequestHeader "Accept", "application/json"
oHttp.Send (mType = OPEN_SYSTEM_TRADE & systemOwnerId = 10)
sHTML = oHttp.ResponseText
Worksheets("Open1").Range("A1").Value = sHTML

发送到网站的预定义格式是json中的描述如下:{"mType":"OPEN_SYSTEM_TRADE","systemOwnerId":10,"systemId":16, etc}

The predefined format to be sent to the website is a description in json as follows : {"mType":"OPEN_SYSTEM_TRADE","systemOwnerId":10,"systemId":16, etc}

我的 oHttp.Send 行肯定是错误的,只要我添加更多参数,就会收到编译器错误我发布了这个(不工作)代码,因为它是迄今为止我在网上能找到的最好的代码(所有其他的让我陷入其他我不明白的事情......

My oHttp.Send line must be wrong, as soon as i add more arguments, i get a compiler error I publish this (not working) code cause its the best i could find on the web so far (all other get me stuck on other things that i don't understand ...

我也尝试将json代码放在​​一个单元格中,将单元格放在一个字符串中,然后像这样发送字符串:oHttp.Send(字符串),结果是一个错误 406 Not Acceptable 来自网站的回复.

I also tried to put the json code in a cell, put the cell in a string, and send the string like this : oHttp.Send (string), which results in a Error 406 Not Acceptable reply from the website.

推荐答案

JSON 对它的格式非常敏感,所以我会确保在发送之前正确引用所有内容.我建议将 Body 拆分为一个单独的变量并使用 http://jsonformatter.curiousconcept 调试该值.com/ 发送前.

JSON can be very sensitive to how it's formatted, so I would make sure everything is quoted properly before it is sent. I would recommend splitting Body into a separate variable and debugging the value with http://jsonformatter.curiousconcept.com/ before sending.

Dim Body As String
Body = "{""mType"":""OPEN_SYSTEM_TRADE"",""systemOwnerId"":10}"

' Set breakpoint here, get the Body value, and check with JSON validator
oHttp.Send Body

我在使用 Salesforce 的 REST API 时遇到了许多类似的问题,并将我的工作合并到一个可能对您有用的库中:https://github.com/VBA-tools/VBA-Web.使用此库,您的示例将如下所示:

I ran into many similar issues when working with Salesforce's REST API and combined my work into a library that may be useful to you: https://github.com/VBA-tools/VBA-Web. Using this library, your example would look like:

Dim Body As New Dictionary
Body.Add "mType", "OPEN_SYSTEM_TRADE"
Body.Add "systemOwnerId", 10

Dim Client As New WebClient
Dim Response As WebResponse
Set Response = Client.PostJson("somewebsite.com", Body)

Worksheets("Open1").Range("A1").Value = Response.Content

这篇关于在 excel vba 中将 JSON 发布到网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆