为什么我的JSON格式与HighCharts示例不同?

时间:2015-09-14 13:31:57

标签: javascript c# jquery json highcharts

当我使用jQuery的$ .GetJSON时,返回的结果如下所示:

{"Points":[{"X":1,"Y":6},{"X":2,"Y":5},{"X":3,"Y":1},{"X":4,"Y":5},{"X":5,"Y":5},{"X":6,"Y":10},{"X":7,"Y":4},{"X":8,"Y":1},{"X":9,"Y":9},{"X":10,"Y":2}]}

但是,Highcharts文档声明输出应如下所示:

[
[1,12],
[2,5],
[3,18],
[4,13],
[5,7],
[6,4],
[7,9],
[8,10],
[9,15],
[10,22]
]

我该如何解决这个问题?

额外信息:我正在使用此示例 http://www.highcharts.com/docs/working-with-data/custom-preprocessing#3

这是我的代码:

的JavaScript     

<script>
    var options = {
        chart: {
            renderTo: 'container',
            type: 'spline'
        },
        series: [{}]
    };

    $(document).ready(function () {
        $.getJSON("/HighchartsTest/JSONTest", function (data) {
            options.series[0].data = data;
            var chart = new Highcharts.Chart(options);
        });
    });
</script>

MVC C#

public JsonResult JSONTest()
{
    return Json(new GenericInfo(), JsonRequestBehavior.AllowGet);
}

public class GenericInfo
{
    public Point[] Points { get; set; }

        public GenericInfo()
        {
            Random r = new Random();
            Points = new Point[10];

            for (int i = 0; i < 10;)
            {
                Points[i] = new Point(++i, r.Next(0, 10) + 1);
            }
        }
    }
}

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

我得到的图表是空的。使用控制台检查图表时,似乎chart.series[0].data数组为空。我似乎无法弄清楚我的代码是否错误,或者它是否只是不接受JSON对象。

1 个答案:

答案 0 :(得分:0)

问题是我发送的是GenericInfo对象而不是列表Points本身。在此之后,我还需要将名称X,Y更改为x,y(非大写字母),这样就修复了图表。