To serialize an object to JSON string, you need to use the method System.Web.Script.Serialization.JavaScriptSerializer.Serialize, and pass your object as an argument. Here is an example (note: in console application project, you might want to add a reference to System.Web):
public class Person {
public string Name {get; set;}
public int Age {get; set;}
public List Contacts {get; set;}
}
public class Program {
static void Main(string[] args){
Person p = new Person {
Name = "Whoever",
Age = 30,
Contacts = new List()
}
p.Contacts.Add("Contact 1");
p.Contacts.Add("Contact 2");
p.Contacts.Add("Contact 3");
var json = new System.Web.Script.Serialization.JavaScriptSerializer()
.Serialize(p);
Console.WriteLine(json);
}
}
Hope that helps :)