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.
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.
[php]
List _EmployeeDetails= new List();
EmployeeDetails user = new EmployeeDetails();
EmployeeDetails.Add(new EmployeeDetails{ EmployeeID= 1, UserName= “Rajesh”, Location= “Chennai” });
string strserialize = JsonConvert.SerializeObject(EmployeeDetails);
[/php]
JSON DeSerialization Method
Following is the serialize method to serialize list items into JSON string format. Again this is achieved by Newtonsoft.
C# Source Code
[php]
string strmsg = “[{\”EmployeeID\”:1,\”UserName\”:\”Rajesh\”,\”Location\”:\”chennai\”},{\”EmployeeID\”:2,\”UserName\”:\”Mahesh\”,\”Location\”:\”Surat\”}]”;
var user = JsonConvert.DeserializeObject<List>(strmsg);
[/php]
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.
[php]
[/php]
Now write below code in code behind file of above .aspx file.
[php]
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 details = new List();
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>(strmsg);
}
}
class EmployeeDetails
{
public int EmployeeID { get; set; }
public string UserName { get; set; }
public string Location { get; set; }
}
}
[/php]
This is how we can convert object list to Json String.
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.