我試圖將JSON字符串轉換爲VB.net對象,以便輕鬆訪問JSON字符串中的數據。將JSON字符串反序列化爲VB.net對象
我的JSON字符串看起來是這樣的:
{
"status": "ok",
"count": 4,
"data": [
{
"clan_id": 500002846,
"nickname": "Azrael",
"id": 500429872
},
{
"clan_id": null,
"nickname": "Azrael0",
"id": 500913252
},
{
"clan_id": 500028112,
"nickname": "Azrael0313",
"id": 504109422
},
{
"clan_id": null,
"nickname": "Azrael7",
"id": 501594186
}
]
}
現在,我想這個字符串反序列化到VB.net對象
我的類定義是:
Public Class Rootobject
Private _data1 As String
Public Property status As String
Public Property count As Integer
Public Property data() As Datum
End Class
Public Class Datum
Public Property clan_id As Integer?
Public Property nickname As String
Public Property id As Integer
End Class
這Visual Studio 2012自動爲我的JSON字符串創建。
我試圖以.json解串器反序列化:
Dim Testobject As Rootobject _
= Global.Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(JSON_String)
與JavaScriptSerializer:
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim Testobject_2 As Rootobject = serializer.Deserialize(Of Rootobject)(JSON_String)
但在這兩種情況下,我只能夠獲得進入「狀態」和「計數」但不是「數據」數組。
我是新來的Visual Basic,所以我讀了很多關於JSON和解串器和其他人有這樣那樣的問題,但大多數解決方案是C#,而不是爲了VB.net
任何想法我可能做錯了?
試過'公共屬性Data()作爲IList的(中Datum)' 然後它說: 無法反序列化當前的JSON對象(例如{「name」:「value」})到類型'System.Collec tions.Generic.IList'1 [WoT_Tool.WoT_Tool + Datum]',因爲該類型需要JSON數組(例如, [1,2,3])來正確地反序列化。 要解決此錯誤,請將JSON更改爲JSON數組(例如[1,2,3]) – Azrael