在excel vba中将JSON发布到web

在excel vba中将JSON发布到web,json,post,excel,vba,Json,Post,Excel,Vba,我想发布一些JSON和一些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 "Conten

我想发布一些JSON和一些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”:“开放系统交易”、“系统所有者ID:10”、“系统ID:16等”

我的
oHttp.Send
行肯定是错误的,只要我添加更多参数,我就会得到一个编译器错误 我发布这段(不起作用)代码是因为它是目前为止我在网络上能找到的最好的代码(所有其他的代码都会让我陷入其他我不理解的事情中


我还尝试将json代码放在单元格中,将单元格放在字符串中,然后像这样发送字符串:
oHttp.send
(字符串),这将导致一个
错误406不可接受
网站回复。

JSON可能对其格式非常敏感,因此我会确保在发送前正确引用所有内容。我建议将
正文
拆分为一个单独的变量,并在发送前调试值

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时,我遇到了许多类似的问题,并将我的工作合并到一个可能对您有用的库中:。使用此库,您的示例如下所示:

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