Program to convert C# class object to JSON using JavaScriptSerializer

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1188
Suppose we have a class object as under

public class ZoneMaster
{
public string Code { get; set; }
public string CountryId { get; set; }
public string CreatedBy { get; set; }
public string Id { get; set; }
public string LastModifiedBy { get; set; }
public string Name { get; set; }
}


And we want to generate the JSON structure for it. So first we need to populate the same with some data as under

ZoneMaster objZone = new ZoneMaster
{
LastModifiedBy = "Test User",
CreatedBy = "Test User",
Name = "East",
Code = "C123K345",
CountryId = "12",
Id = "1"
};


Then write the below program that will convert C# class object to JSON using JavaScriptSerializer

var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = javaScriptSerializer.Serialize(objZone);
Console.WriteLine(jsonString);

Comments or Responses

Login to post response