【问题标题】:Why is my JSON formatting different from HighCharts examples?为什么我的 JSON 格式与 HighCharts 示例不同?
【发布时间】:2015-09-14 13:31:56
【问题描述】:

当我使用 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 对象。

【问题讨论】:

  • 两者都是有效的 JSON。你试过使用 jQuery 的 $.GetJSON 提供的 JSON 吗?
  • 是的,返回的 JSON 是我用于图表的数据,但是图表是空的。
  • 在原帖中添加了额外的信息。
  • 如果你想让你的 json 看起来像那样,那么你需要返回一个 int[][] 而不是 GenericInfo

标签: javascript c# jquery json highcharts


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多