[英]Deserialize JSON Object using VB.Net
我有一个VB.Net Windows应用程序,该应用程序对使用JSON.Net序列化包含在Dictionary<String, String>
的结果的C#Web应用程序进行RESTful WS调用。
来自C#Web应用程序的响应作为JSON字符串返回,如下所示:
"{"8bf370f3-d258-40d3-a659-063db90f5291":"String1","5a0248de-7139-44ba-991b-072d598ff9ab":"String2"}"
在VB.Net端,我使用JSON.Net尝试反序列化响应,但出现此错误:
Error converting value
"{"8bf370f3-d258-40d3-a659-063db90f5291":"String1","5a0248de-7139-44ba-991b-072d598ff9ab":"String2"}"
to type 'System.Collections.Generic.Dictionary`
2[System.String,System.String]'. Path '', line 1, position 1383.
这是VB.Net代码:
..
..
reader = New StreamReader(response.GetResponseStream())
' Console application output
Dim responseStr As String = reader.ReadToEnd
'MsgBox(responseStr)
'Error occurs on the next line.
Dim dict As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(responseStr)
Dim pair As KeyValuePair(Of String, String)
For Each pair In dict
MsgBox("Key: " & pair.Key.ToString & " Value: " & pair.Value)
Next
..
..
我尝试使用“对象”作为类型而不是字符串,但遇到相同的错误。 我看不到JSON本身有什么问题,或者反序列化方法调用没有任何问题,所以我很茫然。 任何建议将不胜感激。
我认为您的JSON需要看起来像这样:
[{"8bf370f3-d258-40d3-a659-063db90f5291":"String1"},{"5a0248de-7139-44ba-991b-072d598ff9ab":"String2"}]
字典是键/值对的集合,并且JSON中没有项目的集合,只有一个具有4个属性的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.