Solved

Deserialize JSON in VB.Net

Posted on 2013-12-20
7
5,109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

636 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