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>"); Response.Write("</br>"); Response.Write(jsonData); Response.Write("</br>"); var objEmp1 = JsonConvert.DeserializeObject<EmpDemo>(jsonData); Response.Write("</br>"); Response.Write("Converting Json to .Net Object:"); Response.Write("</br>"); 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.