2013-12-17 5 views
5

Sto provando a convertire una stringa JSON in un oggetto VB.net per ottenere un accesso semplice ai dati nella stringa JSON.Deserializzazione stringa JSON nell'oggetto VB.net

mio JSON stringa simile a questa:

{ 
    "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 
    } 
    ] 
} 

Ora sto cercando di deserializzare questa stringa in un oggetto VB.net

mie definizioni di classe sono:

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 

che Visual Studio 2012 è stato creato automaticamente per la mia stringa JSON.

ho cercato di deserializzare con .json Deserializer:

Dim Testobject As Rootobject _ 
= Global.Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(JSON_String) 

e con JavaScriptSerializer:

Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer() 
Dim Testobject_2 As Rootobject = serializer.Deserialize(Of Rootobject)(JSON_String) 

Ma in entrambi i casi io sono solo in grado di ottenere l'accesso a "status" e "contare" ma non alla matrice "dati".

Sono nuovo di Visual Basic, così ho letto molto su di JSON e Deserializer e altre persone con questo tipo di problemi, ma la maggior parte sono soluzioni per C# e non per VB.net

Tutte le idee che ho potrebbe aver sbagliato?

+0

dati provato 'pubblica di proprietà() Come IList (Of Datum) Quindi: Impossibile deserializzare l'oggetto JSON corrente (ad esempio {"nome": "valore"}) in tipo 'System.Collec tions.Generic.IList'1 [WoT_Tool.WoT_Tool + Datum] 'perché il tipo richiede un array JSON (ad es. [1,2,3]) per deserializzare correttamente. Per correggere questo errore, cambiare il JSON in un array JSON (ad es. [1,2,3]) – Azrael

risposta

1

ho convertito la tua JSON utilizzando JsonToCSharp ... e poi convertito il C# per 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 

dare a queste classi di una prova.

+0

Con Serializer JSON.net non funziona ma con JavascriptSerializer lo è. Ma poi dice che non ci sono dati nella lista e che è vuoto. – Azrael

0

Sei vicino; hai le parentesi nel posto sbagliato. Nella classe Rootobject, modificare questa riga:

Public Property data() As Datum 

a questo:

Public Property data As Datum() 

Oppure, questo funziona anche:

Public Property data As List(Of Datum)