Serializing data/objects using XML is quite a standard way over the Web. But, XML has it’s own set of disadvantages owing to which I tried serializing objects in JSON format. It seems .NET has in-built support for doing the same. One simply needs to use the JavaScriptSerializer class available within ‘System.Web.Script.Serialization’ namespace.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Script.Serialization; /*Add System.Web.Extension library*/ namespace ConsoleApplication1 { class Program { static void Main(string[] args) { JavaScriptSerializer sz = new JavaScriptSerializer(); List<Employee> empList = new List<Employee>() { new Employee("gaurav", 1), new Employee("gauz", 2) }; Console.WriteLine(sz.Serialize(empList)); Console.ReadLine(); } } class Employee { public string name; public int id; public Employee() { } public Employee(string Name, int ID) { this.id = ID; this.name = Name; } } }
Login