Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6477471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:57:07+00:00 2026-05-25T06:57:07+00:00

i am trying to post complex data structure to my server side method with

  • 0

i am trying to post complex data structure to my server side method with jquery.

here i am giving my c# class structure

public partial class SerializeJSON : System.Web.UI.Page
{

    [System.Web.Services.WebMethod]
    public static string GetData(List<Customer> oCust)
    {
        int empid = oCust[0].EmpID;
        string name = oCust[0].Name.ToString();
        DateTime dob = oCust[0].BirthDate;
        double sal = oCust[0].Salary;
        Address add = oCust[0].Address[0];
        return "";
    }
}

public class Customer
{
     List<Address> add = null;
     public Customer()
     {
         add = new List<Address>();
     }

    public int EmpID { get; set; }
    public string Name { get; set; }
    public DateTime BirthDate { get; set; }

    public List<Address> Address
    {
        get { return add; }
        set { add = value; }
    }
    public double Salary { get; set; }
}

public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string PostCode { get; set; }
}

i know that how right json would look like. the right json would be

[
{
    "EmpID":1,
    "Name":"Keith",
    "BirthDate":"\/Date(947874600000)\/",
    "Address":[{"Address1":"Salt lake","Address2":"Kolkata","PostCode":"784512"}],"Salary":5200
}

]

here i am telling you how to send data actually from my client to server side
this my javascript code by which i am populating data at client side and late i am sending this like

 var Person = {};
    Person["EmpID"] = 1;
    Person["Name"] = "Keith";
    Person["BirthDate"] = "08/15/2011";

    var Address = {};
    Address["Address1"] = "Salt lake";
    Address["Address2"] = "Kolkata";
    Address["PostCode"] = "741258";

    Person["Address"] = Address;

$(function () {
        $('#btnSubmit').click(function () {
            alert(JSON.stringify(Person));
            $.ajax({
                type: "POST",
                url: "SerializeJSON.aspx/GetData",
                data: "{'oCust':'" + JSON.stringify(Person) + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                }
                ,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
            return false;
        });
    });

but i am getting error. JSON.stringify() not generating correct json.
so please tell me what would be the best approach to populate & generate right json data programatically. one thing i need to say that i dont want to generate json manually rather show me the way to generate right json programatically.

so guide me how to generate right json programatically with javascript.

also tell my approach like to populate data with javascript was wrong.

 var Person = {};
    Person["EmpID"] = 1;
    Person["Name"] = "Keith";
    Person["BirthDate"] = "08/15/2011";

    var Address = {};
    Address["Address1"] = "Salt lake";
    Address["Address2"] = "Kolkata";
    Address["PostCode"] = "741258";

    Person["Address"] = Address;

the above code is wrong approach.

so please guide me how to generate right json with javascript which can be easily deserialize at server side. thanks

Here i restructure my code according to naveen

var Persons = [];

var Person = {};
Person[“EmpID”] = 1;
Person[“Name”] = “Keith”;
Person[“BirthDate”] = “08/15/2011”;

var Address = [];
var addr = {};
addr[“Address1”] = “Salt lake”;
addr[“Address2”] = “Kolkata”;
addr[“PostCode”] = “741258”;
Address.push(addr);

Person["Address"] = Address;
Persons.push(Person)

var DTO = { oCust : Persons }
data: JSON.stringify( DTO )

i hope now it will work….isn’t it?

var Persons = [];

var Person = {};
Person[“EmpID”] = 1;
Person[“Name”] = “Keith”;
Person[“BirthDate”] = “08/15/2011”;

var Address = [];
var addr = {};
addr[“Address1”] = “Salt lake”;
addr[“Address2”] = “Kolkata”;
addr[“PostCode”] = “741258”;
Address.push(addr);

Person["Address"] = Address;

var DTO = { oCust : Person }
data: JSON.stringify( DTO )

am i right?

if my class structure would be like below one

public partial class SerializeJSON : System.Web.UI.Page
{

    [System.Web.Services.WebMethod]
    public static string GetData(Customer oCust)
    {
        int empid = oCust.EmpID;
        string name = oCust.Name.ToString();
        DateTime dob = oCust.BirthDate;
        double sal = oCust.Salary;
        Address add = oCust.Address[0];
        return "";
    }
}

public class Customer
{
     List<Address> add = null;
     public Customer()
     {
         add = new List<Address>();
     }

    public int EmpID { get; set; }
    public string Name { get; set; }
    public DateTime BirthDate { get; set; }

    public List<Address> Address
    {
        get { return add; }
        set { add = value; }
    }
    public double Salary { get; set; }
}

public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string PostCode { get; set; }
}

then my json would look like

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:57:07+00:00Added an answer on May 25, 2026 at 6:57 am

    This is a snippet on auto de-serializing complex JSON using a WebMethod.
    Please go through the comments in case of doubts.

    Class

    public class Customer
    {
        public int EmpID { get; set; }
        public string Name { get; set; }
        public DateTime BirthDate { get; set; }
    
        // why did you use the conventional get set in this case?
        // feel free to revert if you are using the class elsewhere. 
        public List<Address> Address { get; set; }
        public double Salary { get; set; }
    }
    
    public class Address
    {
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string PostCode { get; set; }
    }
    

    WebMethod

    [System.Web.Services.WebMethod]
    public static string GetData(Customer oCust)
    {
        try
        {
            int empid = oCust.EmpID;
            string name = oCust.Name.ToString();
            DateTime dob = oCust.BirthDate;
            double sal = oCust.Salary;
            Address add = oCust.Address[0];
            return "Success";
        }
        catch (Exception exception)
        {
            //Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
            return "Failed";
        }
    }
    

    AJAX Call

    $(function () {
        $('#btnSubmit').click(function (evt) {
            var Person = {};
            Person["EmpID"] = 1;
            Person["Name"] = "Keith";
            //converting to date is not mandatory, but its more typesafe
            Person["BirthDate"] = new Date("08/15/2011");
    
            var Address = [];
            var addr = {};
            addr["Address1"] = "Salt lake";
            addr["Address2"] = "Kolkata";
            addr["PostCode"] = "741258";
            Address.push(addr);
    
            Person["Address"] = Address;
    
            var DTO = { oCust: Person }
            //save yourself some typing. dataType and charset in content type are not needed.
            //Courtesy: http://encosia.com/save-yourself-some-typing-when-you-call-asp-net-services/
            $.ajax({
                type: "POST",
                url: "SerializeJSON.aspx/GetData",
                data: JSON.stringify(DTO),
                contentType: "application/json",
                success: function (msg) {
                    //ASP.NET 2.0 fallback.
                    var data = msg.hasOwnProperty("d") ? msg.d : msg;
                    //place a div with id=content to show the result.(not mandatory)
                    $("#content").hide().html(msg.d).delay(1000).show(400);
                },
                error: function (xhr, textStatus, errorThrown) {
                    //you should ideally call the xhr.responseText to see the error
                    alert(xhr.responseText);
                }
            });
            evt.preventDefault();
        });
    });
    

    HTML

    <div id="content"></div>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit"/>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at ASP.NET MVC2 and trying to post a complex object using
I'm trying to POST data to an external url using HttpWebRequest, then i need
I am trying to achieve a JQuery AJAX call to a controller action method
I am trying to POST a HTML (contained in a file) to a URL
I'm trying to POST to the HTTP gateway of an SMS provider (Sybase 365)
I am trying to post an array full of checkboxes and to open it
I am trying to post something on a facebook users feed and then afterwards
I'm trying to post some code on a blog using the pre tag. Something
I'm trying to post to the login form of an application on another subdomain
I'm trying to POST a http request using ajax, but getting a response from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.