Introduction:

In this post we will learn about how to ‘Convert List Object to JSON String in C#‘ in asp.net using C#.Net with example.

Description:

In my previous post I explained various topics like Convert generic list to datatable in Asp.Net C#.Net. Abstract Class in C# with Example,Routing in Asp.Net MVC with Example,Use of Global.asax in Asp.net.

Advantages of JSON

– Smaller message size
– Information are stored in more structural document.
– It can easily distinguish the number and string i.e. 1 and “1”
– Easy to represent NULL value.
json
By using newtonsoft json(Which is available in Nuget Package Manager) we can easily convert the list object to the string which contain corresponding Json.

Below I have provided methods to Convert List Object to JSON String in C#.Net.

JSON Serialization Method:
Below method is use to convert object list to Json formate string.

List<EmployeeDetails> _EmployeeDetails= new List<EmployeeDetails>();
EmployeeDetails user = new EmployeeDetails();
EmployeeDetails.Add(new EmployeeDetails{ EmployeeID= 1, UserName= "Rajesh", Location= "Chennai" });
string strserialize = JsonConvert.SerializeObject(EmployeeDetails);

 

JSON DeSerialization Method

Following is the serialize method to serialize list items into JSON string format. Again this is achieved by Newtonsoft.

C# Code

string strmsg = "[{\"EmployeeID\":1,\"UserName\":\"Rajesh\",\"Location\":\"chennai\"},{\"EmployeeID\":2,\"UserName\":\"Mahesh\",\"Location\":\"Surat\"}]";
            var user = JsonConvert.DeserializeObject<List<EmployeeDetails>>(strmsg);

Below I have demonstrate complete working example for serialization and deserialization for Json Data.
create new web application and open your Default.aspx page and write the code like as shown below.

<form id="frmMain" runat="server">
        <div>
            <asp:Button ID="btnSerialize" runat="server" Text="Serialize" OnClick="btnSerialize_Click" />
            <asp:Button ID="btnDeserialize" runat="server" Text="DeSerialize" OnClick="btnDeserialize_Click" />
            <div>
                Serialized Data:
                <asp:Label ID="lblserial" runat="server" />
            </div>
            <div>
                DeSerialized Data:
                <asp:Label ID="lbldeserial" runat="server" />
            </div>
        </div>
    </form>

Now write below code in code behind file of above .aspx file.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Json
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSerialize_Click(object sender, EventArgs e)
        {
            List<EmployeeDetails> details = new List<EmployeeDetails>();
            EmployeeDetails user = new EmployeeDetails();
            details.Add(new EmployeeDetails { EmployeeID = 1, UserName = "Rajesh", Location = "Chennai" });
            details.Add(new EmployeeDetails { EmployeeID = 1, UserName = "Mahesh", Location = "Surat" });
            details.Add(new EmployeeDetails { EmployeeID = 1, UserName = "Naresh", Location = "Delhi" });
            string strserialize = JsonConvert.SerializeObject(details);
            lblserial.Text = strserialize;
        }

        protected void btnDeserialize_Click(object sender, EventArgs e)
        {
            string strmsg = "[{\"EmployeeID\":1,\"UserName\":\"Rajesh\",\"Location\":\"chennai\"},{\"EmployeeID\":2,\"UserName\":\"Mahesh\",\"Location\":\"Surat\"}]";
            var user = JsonConvert.DeserializeObject<List<EmployeeDetails>>(strmsg);
        }
    }

    class EmployeeDetails
    {
        public int EmployeeID { get; set; }
        public string UserName { get; set; }
        public string Location { get; set; }
    }
}

This is how we can convert object list to Json String.

Read :

Summary

You can also read about ASP.NET,C#.NET, JQUERY, AJAX.
I hope you get an idea about Convert List Object to JSON String in C#.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website