我们有一个在vb6中构建的应用程序,需要重写一些函数才能与我们即将完成的JSON API交谈。 vb6应用程序之前直接与我们正在退役的数据库进行通信。
如果我们可以将查询参数序列化为字符串并通过GET请求将它们发送到查询参数中,那么我们的API会更快乐
例如:
https://my_api_url.com/resource?query=stringified_json_object
vb6本身不会说JSON并依赖库来提供一些帮助,我可以阅读JSON但是如何将其转换回字符串?
答案 0 :(得分:0)
Private Sub Receive(ByVal NewURL As String, NewData() As Byte)
Dim xmlhttp As MSXML2.ServerXMLHTTP
Set xmlhttp = New MSXML2.ServerXMLHTTP
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "GET", NewURL, False
xmlhttp.setRequestHeader "Content-Type", "application/json; charset=utf-8"
xmlhttp.send
NewData = xmlhttp.responseBody
Set xmlhttp = Nothing
End Sub
Private Sub Send(ByVal NewURL As String, ByVal NewBlock As String)
Dim xmlhttp As MSXML2.ServerXMLHTTP
Set xmlhttp = New MSXML2.ServerXMLHTTP
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "PUT", NewURL, True
xmlhttp.setRequestHeader "Content-Type", "application/json; charset=utf-8"
xmlhttp.send ("[" & NewBlock & "]")
Set xmlhttp = Nothing
End Sub
如果您在VB中构建字符串,则将是这样
AuxStr = AuxStr & "{"
'/----------------- CODE ------------------------------
AuxStr = AuxStr & """code"": " & """" & varCode & """"
'/------------- DESCRIPTION --------------------------
AuxStr = AuxStr & "," & """name"": " & """" & varName & """"
AuxStr = AuxStr & "}"
另一个选择是从文本文件获取json