1. How to convert .NET object to JSON string
2. How to convert a JSON string to .NET object
We will be using the following Employee class
public class Employee
{
public string firstName { get; set; }
public string lastName { get; set; }
public string gender { get; set; }
public int salary { get; set; }
}
Replace < with LESSTHAN symbol and > with GREATERTHAN symbol
The following example converts List<Employee> objects to a JSON string. Serialize() method of JavaScriptSerializer class converts a .NET object to a JSON string. JavaScriptSerializer class is present in System.Web.Script.Serialization namespace.
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee employee1 = new Employee
{
firstName = "Todd",
lastName = "Grover",
gender = "Male",
salary = 50000
};
Employee employee2 = new Employee
{
firstName = "Sara",
lastName = "Baker",
gender = "Female",
salary = 40000
};
List<Employee> listEmployee = new List<Employee>();
listEmployee.Add(employee1);
listEmployee.Add(employee2);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string JSONString = javaScriptSerializer.Serialize(listEmployee);
Response.Write(JSONString);
}
}
}
Output :
[{"firstName":"Todd","lastName":"Grover","gender":"Male","salary":50000},{"firstName":"Sara","lastName":"Baker","gender":"Female","salary":40000}]
The following example converts a JSON string to List<Employee> objects. Deserialize() method of JavaScriptSerializer class converts a JSON string to List<Employee> objects.
string jsonString = "[{\"firstName\":\"Todd\",\"lastName\":\"Grover\",\"gender\":\"Male\",\"salary\":50000},{\"firstName\":\"Sara\",\"lastName\":\"Baker\",\"gender\":\"Female\",\"salary\":40000}]";
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
List<Employee> employees = (List<Employee>)javaScriptSerializer.Deserialize(jsonString, typeof(List<Employee>));
foreach(Employee employee in employees)
{
Response.Write("First Name = " + employee.firstName + "<br/>");
Response.Write("Last Name = " + employee.lastName + "<br/>");
Response.Write("Gender = " + employee.gender + "<br/>");
Response.Write("Salary = " + employee.salary + "<br/><br/>");
}
Example using NewtonSoft.Json library
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSON
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Serialization");
// 1. Serialization
Movie movie = new Movie { Id = 1, Title = "Mission Impossible" };
// movie is a object let us convert this to a string by using JsonConvert.SerializeObject
string result = JsonConvert.SerializeObject(movie);
// Converts to string as {"Id":1,"Title":"Mission Impossible"}
Console.WriteLine(result);
Console.WriteLine("\nDeserialization");
// 2. Deserialization
Movie newMovie = JsonConvert.DeserializeObject lessthan Movie greaterthan (result);
// Now string is converted to object
Console.WriteLine("Id : " + newMovie.Id);
Console.WriteLine("Title : " + newMovie.Title);
Console.WriteLine("\nSerialization of collection");
// 3. Serialization of collection
List lessthan Movie greaterthan movies = new List lessthanMovie greaterthan {
new Movie{ Id=1, Title="Titanic" },
new Movie{ Id=2, Title="The martian"},
new Movie{ Id=3, Title="Black panther"} ,
new Movie{ Id=4, Title="Deadpool 2"} ,
};
string collectionResult = JsonConvert.SerializeObject(movies);
Console.WriteLine(collectionResult);
Console.WriteLine("\nDeserialization of collection");
// 4. Deserialization of collection
List lessthan Movie greaterthan newMovies = JsonConvert.DeserializeObject lessthan List lessthan Movie greaterthan greaterthan (collectionResult);
foreach (var item in newMovies)
{
Console.WriteLine("Id : "+item.Id+"\tTitle : "+item.Title);
}
}
class Movie
{
public int Id { get; set; }
public string Title { get; set; }
}
}
}