将2个序列化对象传递给我的ajax函数

时间:2014-08-29 16:41:13

标签: c# json

我想通过ajax(Json)传递一对序列化数据。

的Javascript

self.funcaoAjaxJqueryFlot = function(data) {
        $.ajax({
            url: data.dataParameters.url,
            type: data.dataParameters.type,
            async: data.dataParameters.async,
            dataType: data.dataParameters.dataType,
            success: function (result) {

                var data2014 = {
                    label: "Revenue Chart",
                    data: result,
                    xaxis: 1
                };

                $.plot(data.dataParameters.containerSuccess, [data2014], {
                   // a lot os properties here 
                });
            }
        });
    }

C#方法

    public void RetornaJson()
    {
        var data1 = new[,]
        {
            {CalculaMilisegundos(new DateTime(2014, 1, 1)), 4},
            {CalculaMilisegundos(new DateTime(2014, 2, 1)), 8},
            {CalculaMilisegundos(new DateTime(2014, 3, 1)), 5},
            {CalculaMilisegundos(new DateTime(2014, 4, 1)), 10},
            {CalculaMilisegundos(new DateTime(2014, 5, 1)), 3},
            {CalculaMilisegundos(new DateTime(2014, 6, 1)), 16},
            {CalculaMilisegundos(new DateTime(2014, 7, 1)), 15},
            {CalculaMilisegundos(new DateTime(2014, 8, 1)), 2}
        };

        var data2 = new[,]
        {
            {CalculaMilisegundos(new DateTime(2013, 1, 1)), 3},
            {CalculaMilisegundos(new DateTime(2013, 2, 1)), 5},
            {CalculaMilisegundos(new DateTime(2013, 3, 1)), 3},
            {CalculaMilisegundos(new DateTime(2013, 4, 1)), 11},
            {CalculaMilisegundos(new DateTime(2013, 5, 1)), 4},
            {CalculaMilisegundos(new DateTime(2013, 6, 1)), 13},
            {CalculaMilisegundos(new DateTime(2013, 7, 1)), 9},
            {CalculaMilisegundos(new DateTime(2013, 8, 1)), 5},
            {CalculaMilisegundos(new DateTime(2013, 9, 1)), 2},
            {CalculaMilisegundos(new DateTime(2013, 10, 1)), 3},
            {CalculaMilisegundos(new DateTime(2013, 11, 1)), 2},
            {CalculaMilisegundos(new DateTime(2013, 12, 1)), 1},
        };

        var dados1 = JsonConvert.SerializeObject(data1);
        Response.Write(dados1);
    }

    private double CalculaMilisegundos(DateTime data)
    {
        TimeSpan span = data - new DateTime(1970, 1, 1);
        return span.TotalMilliseconds;
    }

我的期望

我希望结果像这样,但在我的C#方法中,我只传递了一个带有Seri​​alized对象的变量。我怎样才能传递2个变量? (data1序列化和data2序列化)

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将两个数组包装在外部数组中并返回:

    var data = new[] { data1, data2 };
    var dados1 = JsonConvert.SerializeObject(data);
    Response.Write(dados1);