Member_2_2131373
asked on
Deserialize JSON in VB.Net
I'm trying to Deserialize some nested JSON in VB.Net, but having problems with the structure.
JSON is simple
Code to Deserialize as follows, but nothing is deserialized.
Can anyone suggest what I'm doing wrong?
Jim
JSON is simple
{
"A":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"B":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"C":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"D":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"E":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"F":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"}
}
Code to Deserialize as follows, but nothing is deserialized.
Dim sContactJSON As String = {Get JSON from web service}
Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer
Dim Contact As TestJSON = ser.Deserialize(Of TestJSON)(sContactJSON)
Public Class TestJSON
Public RecordX As RecordX
Public Another As String
End Class
Public Class RecordX
Public Sub1 As String
Public Sub2 As String
End Class
Can anyone suggest what I'm doing wrong?
Jim
You need to restructure your classes. Try this change out:
I used the following two utilities to auto-generate the classes from your JSON:
http://json2csharp.com - Convert JSON to C# classes
http://www.developerfusion.com/tools/convert/csharp-to-vb/ - Convert C# to VB.NET
Public Class RecordX
Public Property Sub1() As String
Get
Return m_Sub1
End Get
Set
m_Sub1 = Value
End Set
End Property
Private m_Sub1 As String
Public Property Sub2() As String
Get
Return m_Sub2
End Get
Set
m_Sub2 = Value
End Set
End Property
Private m_Sub2 As String
End Class
Public Class A
Public Property RecordX() As RecordX
Get
Return m_RecordX
End Get
Set
m_RecordX = Value
End Set
End Property
Private m_RecordX As RecordX
Public Property Another() As String
Get
Return m_Another
End Get
Set
m_Another = Value
End Set
End Property
Private m_Another As String
End Class
Public Class RecordX2
Public Property Sub1() As String
Get
Return m_Sub1
End Get
Set
m_Sub1 = Value
End Set
End Property
Private m_Sub1 As String
Public Property Sub2() As String
Get
Return m_Sub2
End Get
Set
m_Sub2 = Value
End Set
End Property
Private m_Sub2 As String
End Class
Public Class B
Public Property RecordX() As RecordX2
Get
Return m_RecordX
End Get
Set
m_RecordX = Value
End Set
End Property
Private m_RecordX As RecordX2
Public Property Another() As String
Get
Return m_Another
End Get
Set
m_Another = Value
End Set
End Property
Private m_Another As String
End Class
Public Class RecordX3
Public Property Sub1() As String
Get
Return m_Sub1
End Get
Set
m_Sub1 = Value
End Set
End Property
Private m_Sub1 As String
Public Property Sub2() As String
Get
Return m_Sub2
End Get
Set
m_Sub2 = Value
End Set
End Property
Private m_Sub2 As String
End Class
Public Class C
Public Property RecordX() As RecordX3
Get
Return m_RecordX
End Get
Set
m_RecordX = Value
End Set
End Property
Private m_RecordX As RecordX3
Public Property Another() As String
Get
Return m_Another
End Get
Set
m_Another = Value
End Set
End Property
Private m_Another As String
End Class
Public Class RecordX4
Public Property Sub1() As String
Get
Return m_Sub1
End Get
Set
m_Sub1 = Value
End Set
End Property
Private m_Sub1 As String
Public Property Sub2() As String
Get
Return m_Sub2
End Get
Set
m_Sub2 = Value
End Set
End Property
Private m_Sub2 As String
End Class
Public Class D
Public Property RecordX() As RecordX4
Get
Return m_RecordX
End Get
Set
m_RecordX = Value
End Set
End Property
Private m_RecordX As RecordX4
Public Property Another() As String
Get
Return m_Another
End Get
Set
m_Another = Value
End Set
End Property
Private m_Another As String
End Class
Public Class RecordX5
Public Property Sub1() As String
Get
Return m_Sub1
End Get
Set
m_Sub1 = Value
End Set
End Property
Private m_Sub1 As String
Public Property Sub2() As String
Get
Return m_Sub2
End Get
Set
m_Sub2 = Value
End Set
End Property
Private m_Sub2 As String
End Class
Public Class E
Public Property RecordX() As RecordX5
Get
Return m_RecordX
End Get
Set
m_RecordX = Value
End Set
End Property
Private m_RecordX As RecordX5
Public Property Another() As String
Get
Return m_Another
End Get
Set
m_Another = Value
End Set
End Property
Private m_Another As String
End Class
Public Class RecordX6
Public Property Sub1() As String
Get
Return m_Sub1
End Get
Set
m_Sub1 = Value
End Set
End Property
Private m_Sub1 As String
Public Property Sub2() As String
Get
Return m_Sub2
End Get
Set
m_Sub2 = Value
End Set
End Property
Private m_Sub2 As String
End Class
Public Class F
Public Property RecordX() As RecordX6
Get
Return m_RecordX
End Get
Set
m_RecordX = Value
End Set
End Property
Private m_RecordX As RecordX6
Public Property Another() As String
Get
Return m_Another
End Get
Set
m_Another = Value
End Set
End Property
Private m_Another As String
End Class
Public Class RootObject
Public Property A() As A
Get
Return m_A
End Get
Set
m_A = Value
End Set
End Property
Private m_A As A
Public Property B() As B
Get
Return m_B
End Get
Set
m_B = Value
End Set
End Property
Private m_B As B
Public Property C() As C
Get
Return m_C
End Get
Set
m_C = Value
End Set
End Property
Private m_C As C
Public Property D() As D
Get
Return m_D
End Get
Set
m_D = Value
End Set
End Property
Private m_D As D
Public Property E() As E
Get
Return m_E
End Get
Set
m_E = Value
End Set
End Property
Private m_E As E
Public Property F() As F
Get
Return m_F
End Get
Set
m_F = Value
End Set
End Property
Private m_F As F
End Class
I used the following two utilities to auto-generate the classes from your JSON:
http://json2csharp.com - Convert JSON to C# classes
http://www.developerfusion.com/tools/convert/csharp-to-vb/ - Convert C# to VB.NET
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks, the VS2012 feature sounds useful, will investigate.
The problem with the example you have provided is that it's fixed to the record content.
There can be a variable number of records, and the A/B/C etc. are variables, not names.
Jim
The problem with the example you have provided is that it's fixed to the record content.
There can be a variable number of records, and the A/B/C etc. are variables, not names.
{
"A":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"B":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"123":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
....{loads more records}
"9":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"XYZ":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"},
"ABC":{"RecordX":{"Sub1":"49","Sub2":"122"},"Another":"12"}
}
Jim
Then joriszwaenepoel's comment (http:#a39731833) is probably what you are after.
ASKER
joriszwaenepoel Posted
I never used these classes,
Is there a way of deserialising without using the classes?
Or perhaps another approach?
Jim
I never used these classes,
Is there a way of deserialising without using the classes?
Or perhaps another approach?
Jim
You can try to deserialize using the ser.DeserializeObject() method.
The result will be an array or List of KeyValuePairs containing the data. You can then probably create the objects yourself using a for each loop.
The result will be an array or List of KeyValuePairs containing the data. You can then probably create the objects yourself using a for each loop.
Dim Contacts As List(Of TestJSON) = ser.Deserialize(Of List(Of TestJSON))(sContactJSON)
or
Dim Contacts() As TestJSON = ser.Deserialize(Of TestJSON())(sContactJSON)