Free programming examples and instructions

C# List to json string

Code and demo for how to convert c# data list to json string

by Athil

Posted on 25 Mar 2017 Category: Asp.net Views: 1200

Edited on 02 Jun 2017


Here I am going to show Ho to convert c# object to JSON string using JavaScriptSerializer Class. for that need to import namespace using System.Web.Script.Serialization;  For that, I am going to create a data modals.

Data Modal

 public class UserDetails
      public string Name { get; set; }
      public string Email { get; set; }
      public string PhoneNumber { get; set; }
      public string SessionTime { get; set; }


On Client Side

<form id="form1" runat="server">
        Enter Name:&nbsp;
        <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
        <br />
        <br />
        Email :<asp:TextBox ID="TxtEmail" runat="server"></asp:TextBox>
&nbsp;<br />
        <br />
        Phone Number :
        <asp:TextBox ID="TxtPhoneNumber" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />

On buttonClick Event

 protected void Button1_Click(object sender, EventArgs e)
        UserDetails ud = new UserDetails();
        ud.Name = TxtName.Text;
        ud.Email = TxtEmail.Text;
        ud.PhoneNumber = TxtPhoneNumber.Text;
        ud.SessionTime = DateTime.Now.ToString();

        string data = new JavaScriptSerializer().Serialize(ud);




Leave a Comment: