VB.NET 将对象转换为 JSON
Posted
技术标签:
【中文标题】VB.NET 将对象转换为 JSON【英文标题】:VB.NET converting Object to JSON 【发布时间】:2019-11-03 06:53:22 【问题描述】:我正在创建 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
我在参数中发送的Object如下:
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
【问题讨论】:
重复的引号是VB如何转义引号的。字符串的内容是"apiOperation":"Some_action",...
。对我来说看起来是正确的,您在另一端接收它时遇到问题吗?
数据应该以 JSON 格式发送,但是,如果我将其粘贴到 JSON 验证器中,它会说这不是有效的 JSON。是的,接收问题。 @克雷格
嗯,是的,如果你按原样粘贴,验证器不会喜欢它。那是因为您将专门为 VB 格式化的字符串粘贴到不支持 VB 的内容中。您需要尝试不使用转义符的字符串 --- 我不记得监视窗口是否适用于此,或者您是否需要使用放大镜监视小部件。不过,我认为问题不在于 json 输出,但通过检查它看起来还可以。你有没有看过另一端的内容?
我刚刚注意到“rawresp”正在根据需要返回响应。问题是转换为 Jarray。我收到此异常:“从 JsonReader 读取 JArray 时出错。当前 JsonReader 项不是数组:StartObject。路径'',第 1 行,位置 1。” @Craig
我发现了问题。函数的返回类型应该是对象类型。所以,我用 Dim array As JObject = JObject.Parse(rawresp) @Craig 替换了 Dim array As JArray = JArray.Parse(rawresp)
【参考方案1】:
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。
【讨论】:
【参考方案2】:不能评论,所以必须把它作为答案!你能否提供更多细节。当你说...
以下是 inputJSON 变量的示例输出:
您能确认一下您认为格式有什么问题吗?根据我有限的经验,它看起来还可以。
下面是来自我的一个项目的代码 sn-p,以及我如何将 JSON 输出处理到流阅读器,然后使用 JsonConvert.DeserializeObject,有点绕,但它完成了这项工作,最终变得更具可读性。
希望对你有帮助, 回复
Function test()
Try
'Get obligations
Dim origResponse As HttpWebResponse
Dim AccessToken As String = Access_Token
Dim origRequest As HttpWebRequest = Nothing
origRequest = DirectCast(HttpWebRequest.Create("https://api.service.hmrc.gov.uk/organisations/vat/" + CoVRN + "/obligations?from=2019-01-01&to=" + Now.ToString("yyyy-MM-dd") + "&status=O"), HttpWebRequest) 'for testing
origRequest.Accept = "application/vnd.hmrc.1.0+json"
origRequest.Headers.Add("Authorization", "Bearer " + AccessToken)
origRequest.Method = "GET"
origResponse = DirectCast(origRequest.GetResponse(), HttpWebResponse)
Dim reader As IO.StreamReader = New IO.StreamReader(origResponse.GetResponseStream(), Text.Encoding.Default)
Dim content As String = reader.ReadToEnd()
Dim myResults = JsonConvert.DeserializeObject(Of RootObligation)(content)
Catch webEx As WebException
Dim errorMessage As String = webEx.Message
Dim errorStack As String = webEx.StackTrace
Dim stream = webEx.Response.GetResponseStream()
Dim reader = New StreamReader(stream)
Dim ReadableError As String = reader.ReadToEnd().ToString
Dim myResults = JsonConvert.DeserializeObject(Of RootError)(ReadableError)
Catch ex As Exception
Dim errorMessage As String = ex.Message
Dim errorStack As String = ex.StackTrace
Session("message") = "Error in submission ex:" + ex.Message + " " + errorStack
Return RedirectToAction("VATSubmission", "Transaction")
End Try
End Function
【讨论】:
以上是关于VB.NET 将对象转换为 JSON的主要内容,如果未能解决你的问题,请参考以下文章
无法将对象类型“System.String[*]”转换为“System.String[]”
ASP.NET vb.net 将月/日/年中的日历日期格式转换为具有日/月/年的日期对象