Section 1: Serializing the Object Into JSON

    A simple Patient object:
      public class Patient
      {
          public string ID { get; set; }
          public string LastName { get; set; }
          public string FirstName { get; set; }
      }

    Adding some instances of patient to a Generic List:
      Patient patient1 = new Patient();
      Patient patient2 = new Patient();
      Patient patient3 = new Patient();
      patient1.ID = "101";
      patient1.FirstName = "Mary";
      patient1.LastName = "Jones";
      patient2.ID = "102";
      patient2.FirstName = "John";
      patient2.LastName = "Smith";
      patient3.ID = "103";
      patient3.FirstName = "Pat";
      patient3.LastName = "Young";
      List pList = new List<>();
      pList.Add(patient1);
      pList.Add(patient2);
      pList.Add(patient3);

Serializing with the JavaSciptSerializer:
System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = jSerializer.Serialize(List);

Section 2:

Deserializing JSON with the JavaSciptSerializer:


Suppose you have a jSONToObject string with the following JSON Name/Value Pairs:

[{"ID":"101","LastName":"Jones","FirstName":"Mary"},
{"ID":"102","LastName":"Smith","FirstName":"John"},
{"ID":"103","LastName":"Young","FirstName":"Pat"}]

List listPatient = jsSerializer.Deserialize>(objToJSON);
for (int i = 0; i < listPatient.Count; i++)
{
   jSONToObj = jSONToObj + "ID: " + listPatient[i].ID.ToString() + Environment.NewLine;
   jSONToObj = jSONToObj + "First Name: " + listPatient[i].FirstName.ToString() + Environment.NewLine;
   jSONToObj = jSONToObj + "Last Name: " + listPatient[i].LastName.ToString() + Environment.NewLine + Environment.NewLine;
}