Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deserialize JSON in VB.Net

Posted on 2013-12-20
7
Medium Priority
?
5,369 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 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

670 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