我正在使用一些用VB6编写的遗留应用程序中的web服务。现在,我已经能够使用这里提供的VB解析器解析从web服务返回的JSON:http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html
但是,我仍然在对传递到POST请求有效负载中的JSON字符串进行硬编码。
一般而言:
result = WebRequestPost(url, "{""Id"":""" & productId & """,""Name"":""" & productName & """,""Category"":""" & productCat & """,""Price"":""" & productPrice & """}")有没有一种更干净的方法可以基于对象生成JSON有效负载?
发布于 2017-08-09 05:26:04
我最终构建了自己的汇编程序...
Dim jsonArray() As String
'_______________________________________________________________
'Initializes the opening and closing braces of the JSON payload
Public Sub JSONInitialize()
ReDim jsonArray(1)
jsonArray(0) = "{"
jsonArray(1) = "}"
End Sub
'_______________________________________________________________
'Adds a string value to the JSON payload
Public Sub JSONAddString(nFieldName As String, nValue As String)
Dim temp As String
temp = jsonArray(UBound(jsonArray))
Dim index As Integer
index = UBound(jsonArray)
ReDim Preserve jsonArray(UBound(jsonArray) + 1)
jsonArray(UBound(jsonArray)) = temp
jsonArray(index) = """" & nFieldName & """:""" & nValue & ""","
End Sub
'_______________________________________________________________
'Adds an integer value to the JSON payload
Public Sub JSONAddInt(nFieldName As String, nValue As Integer)
Dim temp As String
temp = jsonArray(UBound(jsonArray))
Dim index As Integer
index = UBound(jsonArray)
ReDim Preserve jsonArray(UBound(jsonArray) + 1)
jsonArray(UBound(jsonArray)) = temp
jsonArray(index) = """" & nFieldName & """:" & nValue & ","
End Sub所以(清理后的)执行看起来像这样:
Dim o As New MyObject
Call o.JSONInitialize
Call o.JSONAddString("My JSON String Field", "Test String Value")
Call o.JSONAddInt("My JSON Int Field", 25)o.JSONSerialize()返回:
{"My JSON String Field":"Test String Value","My JSON Int Field": 25,}不幸的是,它将逗号放在末尾,所以它不会赢得任何选美比赛,但我调用的API并不关心。
https://stackoverflow.com/questions/45552912
复制相似问题