Serialize a List of Object to JSON in C#

This can be easily be done using Json.NET. Json.NET is a popular high-performance JSON framework for .NET.

First of all, you will need to download the latest binary from here and include it your project.

using Newtonsoft.Json;

Rest of the code is pretty self explanatory…

public class Employee
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

var employees = new List<Employee>
    new Employee {FirstName = "Yasser", LastName = "Shaikh", Age = 26},
    new Employee {FirstName = "Mohsin", LastName = "Shaikh", Age = 22},
    new Employee {FirstName = "Riaz", LastName = "Shaikh", Age = 50}

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(employees);