gpt4 book ai didi

json - 在 Excel 中使用 VBA 时 Firebase REST API 不解析 JSON

转载 作者:行者123 更新时间:2023-12-04 20:30:30 24 4
gpt4 key购买 nike

我在使用其余 API 将 JSON 数据发送到 firebase 数据库时遇到问题,数据已发送,但无法解析。例如,如果我在 Windows 的命令提示符中使用此 curl 命令:

curl -X PUT -d  "{\"lastName\":\"Jones\",\"firstName\":\"Bubba\"}"   https://<database-name>.firebaseio.com/rest/test/.json

这会导致正确解析数据:
Correct Parsing of JSON data

然而,当使用以下 VBA 代码时:
Sub PUSHhttpRequestTest()  'Doesn't Work!!

Dim sc As Object
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"

Dim strURL As String: strURL = "https://<database-name>.firebaseio.com/rest/.json"

Dim strRequest
strRequest = """{\""lastName\"":\""Jones\"",\""firstName\"":\""Bubba\""}"""
Dim XMLhttp: Set XMLhttp = CreateObject("msxml2.xmlhttp")
Dim response As String
Debug.Print strRequest
XMLhttp.Open "PUT", strURL, False
XMLhttp.setrequestheader "Content-Type", "application/json;charset=UTF-8"
XMLhttp.sEnd strRequest
response = XMLhttp.responseText
Debug.Print response
End Sub

这会发送完全相同的字符串化 JSON,并将其添加到 Firebase 数据库中,但是不会解析 JSON 字符串:
enter image description here

我尝试了不同的内容类型和 JSON 字符串的变体,但似乎没有任何效果。谁能解释我如何让 VBA 脚本发送 Firebase 将解析的数据?

谢谢

最佳答案

我找到了一种将 JSON 数据从 excel 发送到 firebase 的可能解决方案,但它没有回答我关于为什么上面发送字符串化 JSON 的 VBA 代码没有在 Firebase 中被解析的问题。我仍然想要一个解决方案,因为我已经有一个从我的数据创建字符串化 JSON 的函数。

使用 VBA-web Library来自 this Stack Overflow post似乎可以解决问题。该示例使用字典存储您的数据,但请 my comment and the subsequent reply关于要发送的 JSON 字符串的格式。 不需要转义码!

json 没有 PUT 和其他请求类型,但您可以轻松地将这些添加到自己中。

上面的等效代码,但使用 VBA-web 库(带有自定义 PutJson 函数)是:

Sub test()

Dim strURL As String: strURL = "https://<database-name>/rest/test/whatwhat/.json"
Dim strRequest As String: strRequest = "{""LastName"":""Jones"",""firstName"":""Bubba""}"

Dim Client As New WebClient
Dim Response As WebResponse
Set Response = Client.PutJson(strURL, strRequest)

ActiveSheet.Range("A1").Value = Response.Content
End Sub

我们最终得到了这个......

correct parsing

快乐的时光!

但是,我仍然想知道为什么看似相同的 curl 和 VBA HTTP 请求会导致 FireBase 中的数据解析不同?

关于json - 在 Excel 中使用 VBA 时 Firebase REST API 不解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51823443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com