UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Asp.net- C# List object to json string

Demo and Code for how to convert C# List object to json string

by Athil


Posted on 27 Jul 2018 Category: Asp.net Views: 71

Edited on 08 Jul 2019


Here I am going to show how to convert C# list To JSON string.

DEMO

For this, I am having a data model of UserDetails.

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

 }

ASPX Code

 <form id="form1" runat="server">
    <div>
    
        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 />
    
    </div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
    </form>

C# Code

 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);
            Response.Write(data);

        }

 



Leave a Comment: