Introduction:
Here we will learn how to convert list object to JSON string
in asp.net using c#, vb.net with
example or how to convert JSON string to generic list in c#, vb.net
with example or convert list of objects to JSON string
in asp.net using c#, vb.net
with example. By using newtonsoft.json reference in our asp.net
applications we can easily convert list object to JSON string or JSON string to list object based
on our requirements.
Description:
In previous articles I explained jQuery scrollable gridview with fixed header example, jQuery countdown timer example, jQuery redirect to another page after 5 seconds, jQuery disable right click on image with example, 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 convert list object to JSON string
in asp.net using c#, vb.net
with example.
By using newtonsoft json we can easily convert list
object to JSON string in asp.net. Following are the methods to convert list object to JSON string and JSON string to List Object 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
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. |
|||
|
|||
3 comments :
please post article on .net core with WEPAPI +sharepoint
And also frond end Angular js 2
Please suggest i am palnning to buid a project with following technologies
we are using Angularjs2+.netcore+Webapi+sharepoint.
Note: Only a member of this blog may post a comment.