Using JavaScriptSerializer to convert C# class object to JSON

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

public class CountryMaster
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

CountryMaster objCountry = new CountryMaster
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(objCountry);

Comments or Responses

Login to post response