JSON Serialization in VB.NET

JSON serialization
 
Lets try to explain a simple serialization of json.
 
First of all lets say we have a json output file which looks like this
 
{"collumns":[{"ID":value,"Name":value,"LastName":value}]} . From this json file we need to make on object.
 
There is a lot of online pages which creates you objects and generate propertys. 
 
The output in both cases should looks like this
  1. Public Class Person 
  2.     Public Property ID() As String  
  3.         Get  
  4.             Return m_ID  
  5.         End Get  
  6.         Set  
  7.             m_ID = Value  
  8.         End Set  
  9.     End Property  
  10.     Private m_ID As Integer  
  11.   
  12.     Public Property Name() As String  
  13.         Get  
  14.             Return m_Name  
  15.         End Get  
  16.         Set  
  17.             m_Name = Value  
  18.         End Set  
  19.     End Property  
  20.     Private m_Name As String  
  21.   
  22.     Public Property LastName() As String  
  23.         Get  
  24.             Return m_Last  
  25.         End Get  
  26.         Set  
  27.             m_Last = Value  
  28.         End Set  
  29.     End Property  
  30.     Private m_Last As String  
  31.   
  32.     Public Class Collectionss  
  33.         Public Property column As Person()  
  34.     End Class  
  35.   
  36. End Class  
After the json classes are generated we need to add the reference for NewtonSoft.Json. 
 
We can add the desired reference by nudget package
 
"Tools -> Nuget package manager -> Package manager console " and type the command
  1. Install-Package Newtonsoft.Json  
 After its downloaded successfully just import it:
  1. Imports Newtonsoft.Json  
I will add some test values for it.
  1. Dim person As New Person()  
  2. Dim persons As New List(Of Person)  
  3.   
  4. person = New Person  
  5.   
  6. With person  
  7.       .id = 25  
  8.       .LastName = "Wayne"  
  9.       .Name = "John"  
  10. End With  
  11.   
  12. Persons.add(person)  
Now when we have the list of persons (objects) we call function ToJson which will return us the String value
  1. Dim smth As String = person.ToJSON(persons) 
 Now everything is set up for first serialization. We will use .SerializeObject method
  1. Public Function ToJSON(rows As List(Of Person)) As String  
  2.     Dim data As New Collectionss  
  3.   
  4.     data = New Collectionss With {.coll = rows.ToArray}  
  5.     ToJSON = JsonConvert.SerializeObject(data)  
  6.     Return ToJSON  
  7. End Function  
And as the output we have the json format. 
 
In next post it will be explained how to send the json to web using socket.