2013-12-17 140 views
5

我試圖將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

任何想法我可能做錯了?

+0

試過'公共屬性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

回答

1

我轉換使用JsonToCSharp您的JSON ...然後轉換的C#來vb.net ...

Public Class Datum 
    Public Property clan_id() As System.Nullable(Of Integer) 
     Get 
      Return m_clan_id 
     End Get 
     Set 
      m_clan_id = Value 
     End Set 
    End Property 
    Private m_clan_id As System.Nullable(Of Integer) 
    Public Property nickname() As String 
     Get 
      Return m_nickname 
     End Get 
     Set 
      m_nickname = Value 
     End Set 
    End Property 
    Private m_nickname As String 
    Public Property id() As Integer 
     Get 
      Return m_id 
     End Get 
     Set 
      m_id = Value 
     End Set 
    End Property 
    Private m_id As Integer 
End Class 

Public Class RootObject 
    Public Property status() As String 
     Get 
      Return m_status 
     End Get 
     Set 
      m_status = Value 
     End Set 
    End Property 
    Private m_status As String 
    Public Property count() As Integer 
     Get 
      Return m_count 
     End Get 
     Set 
      m_count = Value 
     End Set 
    End Property 
    Private m_count As Integer 
    Public Property data() As List(Of Datum) 
     Get 
      Return m_data 
     End Get 
     Set 
      m_data = Value 
     End Set 
    End Property 
    Private m_data As List(Of Datum) 
End Class 

給這些類一試。

+0

使用JSON.net序列化程序無法正常工作,但使用的是JavascriptSerializer。 但是,它說,列表中沒有數據,而且它是空的。 – Azrael

0

你很近;你的圓括號在錯誤的地方。在你Rootobject類,改變這一行:

Public Property data() As Datum 

這樣:

Public Property data As Datum() 

或者,這也將工作:

Public Property data As List(Of Datum) 
相關問題