麻烦将JSON字符串化数组传递给PageMethod

时间:2011-03-02 17:42:22

标签: c# jquery json

我无法将JSON字符串化数组传递给PageMethod

[{
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "8",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "9",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "5",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "13",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "6",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}, {
    "StartDate": "3/1/2011",
    "EndDate": "3/31/2011",
    "UserId": "11",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}]

当我得到这个ajax请求时,'jsonText'包含上面列出的数据

    function GetUserSchedules() {           
        var jsonText = $.toJSON(arrParams);
        $.ajax({
            type: "POST",
            url: "/myurl/jquery.aspx/GenerateUserSchedules",
            data: "{" + jsonText + "}",
            contentType: "application/json",
            dataType: "json",
            success: AjaxSucceeded
            ,
            error: AjaxFailed
        });
    }

Pagemethod:

    [System.Web.Script.Services.ScriptMethod]
    [System.Web.Services.WebMethod]
    public static void GenerateUserSchedules(Data[] data)
    {
    //do stuff; will return data but for now, just keeping it like this
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();       
}

DataClass:

[Serializable]
public class Data
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int UserID { get; set; }
    public string ViewSelectedValue { get; set; }
    public string ViewSelectedItem { get; set; }
    public string OrgSelectedValue { get; set; }
}

每次发送ajax请求时,都会执行错误功能。我做错了什么?

1 个答案:

答案 0 :(得分:2)

这是日期的常见问题。 JavaScriptSerializer需要以下格式的日期才能成功解析它们:

{
    "StartDate": "\/Date(983401200000)\/",
    "EndDate": "\/Date(985989600000)\/",
    "UserId": "8",
    "DdlViewSelectedValue": "zzz#",
    "DdlViewSelectedItem": "zzz",
    "DdlOrgSelectedValue": "8"
}

其中983401200000表示自1970年1月1日以来的通用协调时间(UTC)的毫秒数。

来自documentation

的引用
  

Date对象,以JSON表示为   “\ /日期(刻度数)\ /”。该   刻度数是正数或   负长值表示   滴答数(毫秒)   从01年午夜开始已经过去了   1970年1月,UTC。

     

支持的最大日期值是   MaxValue(12/31/9999 11:59:59 PM)和   支持的最小日期值是   MinValue(1/1/0001 12:00:00 AM)。