Solved

Deserialize JSON in VB.Net

Posted on 2013-12-20
7
4,307 Views
Last Modified: 2014-01-07
I'm trying to Deserialize some nested JSON in VB.Net, but having problems with the structure.

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

Open in new window


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

Open in new window


Can anyone suggest what I'm doing wrong?



Jim
0
Comment
Question by:StuckInTheMud
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:joriszwaenepoel
Comment Utility
I never used these classes, but it looks like you're trying to deserialze a List Or Array of objects, so maybe you need to schaneg your code to something like theis:

Dim Contacts As List(Of TestJSON) = ser.Deserialize(Of List(Of TestJSON))(sContactJSON)


or

Dim Contacts() As TestJSON = ser.Deserialize(Of TestJSON())(sContactJSON)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
You need to restructure your classes. Try this change out:

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

Open in new window


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
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
P.S.

I just found out from searching that according to the following:

http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx

...you can actually paste a new class into your project if you have a JSON string in your clipboard. Unfortunately, I do not have the option in my menu within Visual Studio 2012, but in glancing around the web it appears that an update to VS may be required. You might check to see if you have the option.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:StuckInTheMud
Comment Utility
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.

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

Open in new window



Jim
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Then joriszwaenepoel's comment (http:#a39731833) is probably what you are after.
0
 

Author Comment

by:StuckInTheMud
Comment Utility
joriszwaenepoel Posted
I never used these classes,


Is there a way of deserialising without using the classes?

Or perhaps another approach?



Jim
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Maintain selection of the datagrdview 8 18
recursion example 16 66
System32Int Error 8 44
Not needed 13 53
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now