Description:
In this example we
explain that how to convert List Object to JSON string in asp.net using C#.or
how to convert generic List Objet to JSON string in C#.or how to convert List
to JSON format using C#.or how to fetch data as a JSON string from List object
in C#.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="SWS.test1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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>
Aspx.cs:
protected void Page_Load(object
sender, EventArgs e)
{
StringBuilder
lstfiles = new StringBuilder();
lstfiles.Append("<img
src='abc.jpg' /><a href='#'>click</a><br>");
string
s = lstfiles.ToString();
abc.InnerHtml = s;
}
protected
void btnSerialize_Click(object sender, EventArgs e)
{
List<employeedetails> details = new List<employeedetails>();
employeedetails
employee = new employeedetails();
details.Add(new employeedetails {
employeeid = 1, employeename = "kirit",
location = "Rajkot" });
details.Add(new employeedetails {
employeeid = 2, employeename = "Pintu",
location = "Ahmedabad" });
details.Add(new employeedetails {
employeeid = 3, employeename = "Punit",
location = "Surat" });
details.Add(new employeedetails {
employeeid = 4, employeename = "Rahul",
location = "Junagadh" });
string
strserialize = JsonConvert.SerializeObject(details);
lblserial.Text = strserialize;
}
protected
void btnDeserialize_Click(object sender, EventArgs
e)
{
string
strmsg = "[{\"employeeid\":1,\"employeename\":\"kirit\",\"location\":\"Rajkot\"},{\"employeeid\":2,\"employeename\":\"Pintu\",\"location\":\"Ahmedabad\"}]";
var
employee = JsonConvert.DeserializeObject<List<employeedetails>>(strmsg);
}
class employeedetails
{
public
int employeeid { get;
set; }
public
string employeename { get;
set; }
public
string location { get;
set; }
}
0 comments:
Post a Comment