我正在创建 POST 类型的 Web 请求,但转换后的 JSON 格式不正确。以下是我的功能:
Public Function CreateWebRequestPOST(ByVal strURL As String, objInput As Object) As JArray
Try
'Serialize the posted data & convert to bytes
Dim inputJson = (New JavaScriptSerializer()).Serialize(objInput)
Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputJson)
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/json"
request.Accept = "application/json"
request.ContentLength = bytes.Length
request.Expect = "application/json"
request.GetRequestStream().Write(bytes, 0, bytes.Length)
Dim username = "username"
Dim password = "passoword"
request.Credentials = New NetworkCredential(username, password)
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim reader As StreamReader
Dim rawresp As String
reader = New StreamReader(response.GetResponseStream())
rawresp = reader.ReadToEnd()
Dim array As JArray = JArray.Parse(rawresp)
reader.Close()
response.Close()
Return array
End Using
Catch ex As Exception
Dim empty As New JArray
Return empty
End Try
End Function
我在参数中发送的对象如下:
Dim objReq As New RequestBodyList
Dim orderlist As New OrderList
orderlist.currency = "test"
orderlist.id = "test"
orderlist.amount = 100
objReq.apiOperation = "some_action"
objReq.order = orderlist
Dim response = main.CreateWebRequestPOST("some_URL", objReq)
Public Class RequestBodyList
Public Property apiOperation() As String
Public Property order() As New OrderList
End Class
Public Class OrderList
Public Property currency() As String
Public Property id() As String
Public Property amount() As Integer
End Class
下面是 inputJSON
变量的示例输出:
"{""apiOperation"":""Some_action"",""order"":{""currency"":""USD"",""id"":""test1234"",""amount"":100}}"
转换后的 JSON 格式似乎不正确。 这里做错了什么? 是什么导致出现双引号?
这是请求正文的发送方式:
请求正文(JSON 对象)
{
"apiOperation": "some_action",
"order": {
"currency": "USD",
"id": "some_order_id" ,
"amount": 50
}
}
最佳答案
Public Function CreateWebRequestPOST(ByVal strURL As String, objInput As Object) As JObject
Try
'Serialize the posted data & convert to bytes
Dim inputJson = (New JavaScriptSerializer()).Serialize(objInput)
Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputJson)
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/json"
request.Accept = "application/json"
request.ContentLength = bytes.Length
request.Expect = "application/json"
request.GetRequestStream().Write(bytes, 0, bytes.Length)
Dim username = "username"
Dim password = "passoword"
request.Credentials = New NetworkCredential(username, password)
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim reader As StreamReader
Dim rawresp As String
reader = New StreamReader(response.GetResponseStream())
rawresp = reader.ReadToEnd()
Dim array As JObject = JObject.Parse(rawresp)
reader.Close()
response.Close()
Return array
End Using
Catch ex As Exception
End Try
End Function
最终意识到返回类型应该是 Object 类型而不是 Jarray 类型。
关于VB.NET 将对象转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56686195/