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
Public Class Person
Public Property ID() As String
Get
Return m_ID
End Get
Set
m_ID = Value
End Set
End Property
Private m_ID As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property LastName() As String
Get
Return m_Last
End Get
Set
m_Last = Value
End Set
End Property
Private m_Last As String
Public Class Collectionss
Public Property column As Person()
End Class
End ClassAfter 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
Install-Package Newtonsoft.Json
After its downloaded successfully just import it:Imports Newtonsoft.Json
I will add some test values for it.
Dim person As New Person()
Dim persons As New List(Of Person)
person = New Person
With person
.id = 25
.LastName = "Wayne"
.Name = "John"
End With
Persons.add(person)
Now when we have the list of persons (objects) we call function ToJson which will return us the String valueDim smth As String = person.ToJSON(persons)
Now everything is set up for first serialization. We will use .SerializeObject method
Public Function ToJSON(rows As List(Of Person)) As String
Dim data As New Collectionss
data = New Collectionss With {.coll = rows.ToArray}
ToJSON = JsonConvert.SerializeObject(data)
Return ToJSON
End FunctionAnd as the output we have the json format. In next post it will be explained how to send the json to web using socket.
