Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net JSON Serialization And Deserialization in C#, VB.NET

Jan 31, 2017
Introduction:

Here we will learn how to implement JSON serialization and deserialization in asp.net using c#, vb.net with example or Use newtonsoft.json for JSON serialization and deserialization in asp.net using c#, vb.net with example or serializing and deserializing JSON data in asp.net using c#, vb.net with example. By using newtonsoft.json reference in our asp.net applications we can easily serialize and deserialize data based on our requirements.

Description:

In previous articles I explained Jsontextwriter example in c#, vb.net, jQuery convert JSON data to HTML table example, jQuery ajax json example in asp.net, convert data table to json string with example, convert string xml to datatable in c#, vb.net, convert json string to json object with example and many more articles related to in JSON, asp.net, mvc, c#, vb.net. Now I will explain how to use newtonsoft.json to serialize and deserialize JSON data in asp.net using c#, vb.net with example.

By using newtonsoft json we can easily serialize and deserialize data based on our requirements. Following are the methods to serialize and deserialize data in asp.net

JSON Serialization Method

Following is the serialize method to serialize list items into JSON string format.

C# Code


List<userdetails> details = new List<userdetails>();
userdetails user = new userdetails();
details.Add(new userdetails { userid = 1, username = "suresh", location = "chennai" });
string strserialize = JsonConvert.SerializeObject(details);

VB.NET Code


Dim details As New List(Of userdetails)()
Dim user As New userdetails()
details.Add(New userdetails() With {
.userid = 1,
.username = "suresh",
.location = "chennai"
})
Dim strserialize As String = JsonConvert.SerializeObject(details)

JSON DeSerialization Method

Following is the serialize method to serialize list items into JSON string format.

C# Code


string strmsg = "[{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"},{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}]";
var user = JsonConvert.DeserializeObject<List<userdetails>>(strmsg);

VB.NET Code


Dim strmsg As String = "[{""userid"":1,""username"":""suresh"",""location"":""chennai""},{""userid"":2,""username"":""rohini"",""location"":""guntur""}]"
Dim user = JsonConvert.DeserializeObject(Of List(Of userdetails))(strmsg)

If you want complete example to implement serialization and deserialization for JSON data create new web application and open your Default.aspx page and write the code like as shown below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JSON Serialization and Deserialization in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<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>
</form>
</body>
</html>
Now open code behind file and write the code like as shown below

C# Code


using System;
using System.Collections.Generic;
using Newtonsoft.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<userdetails> details = new List<userdetails>();
userdetails user = new userdetails();
details.Add(new userdetails { userid = 1, username = "suresh", location = "chennai" });
details.Add(new userdetails { userid = 2, username = "rohini", location = "guntur" });
details.Add(new userdetails { userid = 3, username = "praveen", location = "bangalore" });
details.Add(new userdetails { userid = 4, username = "sateesh", location = "vizag" });
details.Add(new userdetails { userid = 5, username = "madhav", location = "nagpur" });
details.Add(new userdetails { userid = 6, username = "honey", location = "nagpur" });
string strserialize = JsonConvert.SerializeObject(details);
lblserial.Text = strserialize;
}

protected void btnDeserialize_Click(object sender, EventArgs e)
{
string strmsg = "[{\"userid\":1,\"username\":\"suresh\",\"location\":\"chennai\"},{\"userid\":2,\"username\":\"rohini\",\"location\":\"guntur\"}]";
var user = JsonConvert.DeserializeObject<List<userdetails>>(strmsg);
}
}
class userdetails
{
public int userid { get; set; }
public string username { get; set; }
public string location { get; set; }
}

VB.NET Code


Imports Newtonsoft.Json
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

End Sub
Protected Sub btnSerialize_Click(sender As Object, e As EventArgs)
Dim details As New List(Of userdetails)()
Dim user As New userdetails()
details.Add(New userdetails() With {
.userid = 1,
.username = "suresh",
.location = "chennai"
})
details.Add(New userdetails() With {
.userid = 2,
.username = "rohini",
.location = "guntur"
})
details.Add(New userdetails() With {
.userid = 3,
.username = "praveen",
.location = "bangalore"
})
details.Add(New userdetails() With {
.userid = 4,
.username = "sateesh",
.location = "vizag"
})
details.Add(New userdetails() With {
.userid = 5,
.username = "madhav",
.location = "nagpur"
})
details.Add(New userdetails() With {
.userid = 6,
.username = "honey",
.location = "nagpur"
})
Dim strserialize As String = JsonConvert.SerializeObject(details)
lblserial.Text = strserialize
End Sub
Protected Sub btnDeserialize_Click(sender As Object, e As EventArgs)
Dim strmsg As String = "[{""userid"":1,""username"":""suresh"",""location"":""chennai""},{""userid"":2,""username"":""rohini"",""location"":""guntur""}]"
Dim user = JsonConvert.DeserializeObject(Of List(Of userdetails))(strmsg)
End Sub
End Class

Public Class userdetails
Public Property userid() As Integer
Get
Return m_userid
End Get
Set
m_userid = Value
End Set
End Property
Private m_userid As Integer
Public Property username() As String
Get
Return m_username
End Get
Set
m_username = Value
End Set
End Property
Private m_username As String
Public Property location() As String
Get
Return m_location
End Get
Set
m_location = Value
End Set
End Property
Private m_location As String
End Class

If you observe above code we added namespace “Newtonsoft.JSON” this we can get by adding reference using Manage Nuget Packages. To add reference right click on your application à select Manage Nuget Packages à Go to Browse Tab à Search for Newtonsoft à From the list select Newtonsoft.Json and install it. Once we install the component that will show like as shown following.

Demo

Now run the application to see the result that will be like as shown below.

Following is the result of Serializing Data

Following is the result of DeSerializing the JSON Data

This is how we can implement JSON serialization and deserialization in asp.net using c#, vb.net.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Anonymous said...

Good one.

Anonymous said...

Nice Explaination, Thank you

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.