Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Deserialize JSON in VB.Net

Posted on 2013-12-20
7
Medium Priority
?
5,628 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
ID: 39731833
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 75

Expert Comment

by:käµfm³d 👽
ID: 39732178
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 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 39732192
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:StuckInTheMud
ID: 39735650
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 75

Expert Comment

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

Author Comment

by:StuckInTheMud
ID: 39737518
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
ID: 39737548
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

971 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