通过$ .ajax将嵌套的JSON发送到服务器

时间:2015-07-20 02:06:52

标签: jquery asp.net ajax json nested

我有一个标题(创建以下部分):

"CustomerID": "480","ParentID": 0,"StartDate": "08/07/2015",
"EndDate": "10/7/2015", "TotalCost": 0

以及创建此部分的详细信息表:

               {
                    "selected": "true",
                    "ParentLevelName": "Parent",
                    "ItemName": "Print",
                    "Standard_Cost": "100",
                    "Client_Cost": "100",
                    "id_package": "2",
                    "Stock_Id": "2",
                    "id_Header": "0",
                    "selectedBefore": "false",
                    "id_detail": "0"
                },
                {
                    "selected": "true",
                    "ParentLevelName": "Parent",
                    "ItemName": "Social",
                    "Standard_Cost": "150",
                    "Client_Cost": "150",
                    "id_package": "3",
                    "Stock_Id": "3",
                    "id_Header": "0",
                    "selectedBefore": "false",
                    "id_detail": "0"
                }

当我在一个Json字符串中将它们连接在一起时,它将变为如下:

{
    "CustomerID": "480",
    "ParentID": 0,
    "StartDate": "08/07/2015",
    "EndDate": "12/07/2015",
    "TotalCost": 0,
    "SaleType": 4,
    "ClientRef": "",
    "PackageType": "Package_Type1",
    "Data": [
        [
            {
                "selected": "true",
                "ParentLevelName": "Parent",
                "ItemName": "Print",
                "Standard_Cost": "100",
                "Client_Cost": "100",
                "id_package": "2",
                "Stock_Id": "2",
                "id_Header": "0",
                "selectedBefore": "false",
                "id_detail": "0"
            },
            {
                "selected": "true",
                "ParentLevelName": "Parent",
                "ItemName": "Social",
                "Standard_Cost": "150",
                "Client_Cost": "150",
                "id_package": "3",
                "Stock_Id": "3",
                "id_Header": "0",
                "selectedBefore": "false",
                "id_detail": "0"
            },
          ...
        ]
    ]
}

现在,我想将它发布到服务器,使用$ .ajax,如下所示: (param是我之前提到的JSON字符串)

$.ajax({
       type: "POST",
       url: "Register.aspx/Save",
       contentType: "application/json; charset=utf-8",
       //data: JSON.stringify(param),
       data: param,
       dataType: "json",
       success: function (response) {
       alert("success");
          },
       failure: function (response) {
             alert(response.d);
       }
  });

更新

public class RegisterItem
{
    public bool selected { get; set; }
    public bool selectedBefore { get; set; }  
    public string ParentLevelName { get; set; } 
    public string ItemName { get; set; } 
    public int Standard_Cost { get; set; }
    public int Client_Cost { get; set; }
    public int id_package { get; set; } 
    public int Stock_Id { get; set; }
    public Int64 id_Header { get; set; } 
    public Int64 id_detail { get; set; } 
}

public class RegisterHeader
{
   public Int64 id { get; set; } 
   public Int64? Parent_Id { get; set; }
   public int CustomerID { get; set; } 
   public DateTime Start_Date { get; set; }
   public DateTime End_Date { get; set; }
   public int Sale_Type { get; set; }
   public int TotalCost { get; set; }
   public string Client_Reference { get; set; }
   public string PackageType { get; set; }
   public RegisterItem[] Data { get; set; }
...
}

在page.Cs

[System.Web.Services.WebMethod]
public static RegisterHeader Save(string Data)
{
    return null;
}

UPDATE2: 错误:  反序列化不支持类型'System.String'    阵列

我认为问题是我不知道如何序列化嵌套对象,错误发生在将数据[]添加到标题时。

1 个答案:

答案 0 :(得分:0)

问题是我在c#代码中的参数dataType,

1)我需要在serverSide中专门定义每种类型,也为DateTime定义,我发送字符串并在服务器端代码中解析它。 2)我使用了douglascrockford的decycle

[System.Web.Services.WebMethod]
    public static RegisterHeader Save(string CustomerID, int Parent_Id, string Start_Date,
            string End_Date, int TotalCost, int Sale_Type, string Client_Reference, string PackageType, RegisterItem[] Data)
    {
        return null;
    }

注意:我找不到任何其他解决方案来获取ServerSide中的数据。如果有人能做得更好我会很乐意改变解决方案。 谢谢

<强>更新

在下面的代码中,我将参数类型更改为Header Class: c#代码:

public static RegisterHeader Save(RegisterHeader RegisterHeader)
{/*...*/}

JQuery代码

  //change string to Date
  var from = $('#<% = dtStart.ClientID %>').val().split("/");
  var to = $('#<% = dtEnd.ClientID %>').val().split("/");
  var RegisterHeader = {
                id: ...,
                CustomerID: ...,
                Parent_Id: ...,
                Start_Date: new Date(from[2], from[1] - 1, from[0]), 
                End_Date: new Date(to[2], to[1] - 1, to[0]),
                TotalCost: 0,
                Sale_Type: 4,
                Client_Reference: ...,
                PackageType: ...,
                Data: CreateObjArray() //this is the function which create array of Detail objects 
            };
 var myString = JSON.stringify(JSON.decycle(RegisterHeader));


//----------------------------------------------------------------

//and $.ajax function should be like this:

var param = "{RegisterHeader:" + myString + "}";

                $.ajax({
                    type: "POST",
                    url: "Register.aspx/Save",
                    contentType: "application/json; charset=utf-8",
                    data: param,
                    dataType: "json",
                    success: function (response) {
                        alert("success");
                    },
                    failure: function (response) {
                        alert(response.d);
                    }
                });
                e.preventDefault();
                return false;
            });