我们有一个在vb6中构建的应用程序,需要重写一些函数才能与我们即将完成的JSON API交谈。之前直接与我们的某个数据库通信的vb6应用程序正在退役。
如果我们可以将查询参数序列化为字符串并通过GET请求将它们发送到查询参数中,那么我们的API会更加快乐
例如:https://my_api_url.com/resource?query=stringified_json_object
vb6本身不会说JSON并依赖于库来提供一些帮助,我可以读取JSON但是如何将其转换回字符串?
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