簡體   English   中英

使用VB.Net反序列化JSON對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM