Convert Serialize List Object to JSON String in C# ASP.NET

Convert Serialize List Object to JSON String in C# ASP.NET

Today, We want to share with you Convert Serialize List Object to JSON String in C# ASP.NET.
In this post we will show you c# – Converting list of objects to json array, hear for Convert List Object to JSON String in C# with example we will give you demo and example for implement.
In this post, we will learn about Converting Object to JSON and JSON to C# Objects with an example.

Introduction: Convert List Object to JSON String

Convert List Object to JSON String in C#

In this post we will learn about how to ‘Convert List Object to JSON String in C#‘ in asp.net using Most Useful Tips and Tricks for Microsoft Visual Studio 2013 with example.

Description:

In my previous post I explained various topics like Angular GridView Insert Update and Delete in ASP.NET MVC. ASP.NET URL Routing Web Forms Application, Introduction To ASP.NET Delegates and Events in C#, jQuery Display Image preview before upload in Asp.net.

READ :  ngTouch event example using angularjs-ngtouch directive

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.

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 ASP.NET String in Technology.

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

READ :  PHP Compress Image File Resize while Uploading

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

C# Source 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 :  Simple Pagination With AngularJS and JSON using PHP

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about c# – Converting list of objects to json array.I would like to have feedback on my Infinityknow.com 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.

Add a Comment

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