Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC) (Part 3)


Method 1: Using System.Runtime.Serialization.Json Namespace

Method 2: Using System.Web.Script.Serialization Namespace

Method 3:

In this method we will use the Newtonsoft.Json dll

This is one of the most popular open source dll, we install it from Nuget package manager or from google download it and add to our project

We can write code as given below

using Newtonsoft.Json;
using System;

namespace WebApplication1
    public partial class Demo3 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            EmpDemo objEmp = new EmpDemo
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"

            string jsonData = JsonConvert.SerializeObject(objEmp);
            Response.Write("<b>Converting to Json </b> " + "</br>");

            var objEmp1 = JsonConvert.DeserializeObject<EmpDemo>(jsonData);
            Response.Write("Converting Json to .Net Object:");
            Response.Write("Id: " + objEmp1.Id + "</br>");
            Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
            Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");

        public class EmpDemo
            public int Id { get; set; }
            public string EmpName { get; set; }
            public string EmpAddress { get; set; }

Note: donot forget to include the Newtonsoft.Json namespace in your code. This approach can be used in Asp.net or asp.net mvc application.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.