Json Serialization and Deserialization in ASP.NET

JSON Serialization

  1. Dim str As String = ""  
  2. Dim sr As StreamReader = New StreamReader("Path of Json File")  
  3. str = sr.ReadToEnd()  
  4. str = JsonConvert.SerializeObject(str.ToString)  
JSON is one kind of data format which is designer for running JavaScript on websites.

JSON Deserialization
  1. Dim emp As New Class1  
  2. emp = JsonConvert.DeserializeObject(Of Class1)(str)  
  3. Dim cont As New List(Of Class2)  
  4. cont = JsonConvert.DeserializeObject(Of List(Of Class2))(emp.employees.ToString)  
  5. Response.Write(emp.employees.ToString)  
Public Class
  1. Public Class Class1  
  2. Public Property fname As String  
  3. Public Property employees As Object  
  4. End Class  
  5. Public Class Class2  
  6. Public Property firstName As String  
  7. Public Property lastName As String  
  8. Public Property data As Object  
  9. Public Property phone As Object  
  10. End Class  
  11. Json File  
  12. {  
  13.     "fname""Manish",  
  14.     "employees": [{  
  15.         "firstName""Jatin",  
  16.         "lastName""Chhodavadiya",  
  17.         "data": [{  
  18.             "id": 1,  
  19.             "text""Manish"  
  20.         }],  
  21.         "phone": [{  
  22.             "offece": 9727899812,  
  23.             "home""0275281566"  
  24.         }]  
  25.     }, {  
  26.         "firstName""Ashmita",  
  27.         "lastName""Devani",  
  28.         "data": [{  
  29.             "id": 2,  
  30.             "text""Ashmita"  
  31.         }, {  
  32.             "id": 21,  
  33.             "text""Pipaliya"  
  34.         }],  
  35.         "phone": [{  
  36.             "offece": 9924228344,  
  37.             "home""0275285688"  
  38.         }]  
  39.     }]  
  40. }
Thank you