TypeError:无效' in'使用json文件时的操作数a

时间:2014-07-17 10:05:32

标签: jquery json

我一直在尝试使用json文件来绘制数据图,但是我收到以下错误。我不确定它是我的json文件还是导致此问题的图表编码。 以下是json文件:

{"Real":{
         "data":[[140238120000,50],[140281680000,200],[140575800000,100],
                 [140584800000,150],[140731740000,140],[140809740000,130],
                 [141112530000,120],[141121560000,90],[141260520000,100]]
        }}

代码摘录

var seriesArr = [];
                        $.each('powerdata.json', function (key, data) {
                            var series = {
                                name: key,
                                data: [],
                            };

                            $.each(data.y, function (index, value) {
                                series.data.push({
                                    y: value
                                });
                            });

                        /*  $.each(data.data, function (index, value) {
                                series.data[index] = value;
                            }); */

                            seriesArr.push(series);

                            var seriesValues = {
                                name: series.name,
                                data: series.data,
                                pointInterval: weekInterval,
                                pointStart: Date.UTC(2014, 06, 1, 10, 0, 0)
                            };


                            charts.xAxis[0].axisTitle.attr({
                                text: 'By Week'
                            });

                            charts.addSeries(seriesValues, false);
                            charts.redraw();
                        });

EDITED VERSION

我一直致力于将JSON文件设置为图形。以下是我试过但我不明白为什么这不起作用

JSON文件

[{"name": "Real", "data":[["time" : 140238120000, "val":50],["time" : 140281680000, "val":200],["time" : 140575800000, "val":100],["time" : 140584800000, "val":150],["time" : 140731740000, "val":140],["time" : 140809740000, "val":130],["time" : 141112530000, "val":120],["time" : 141121560000, "val":90],["time" : 141260520000, "val":100]]}]

[{"name": "Real", "data":[["time" : 140238120000, "val":50],["time" : 140281680000, "val":200],["time" : 140575800000, "val":100],["time" : 140584800000, "val":150],["time" : 140731740000, "val":140],["time" : 140809740000, "val":130],["time" : 141112530000, "val":120],["time" : 141121560000, "val":90],["time" : 141260520000, "val":100]]}]

代码

$.getJSON('data.json', function(aData) {
                        var seriesData = [];
                                $.each(aData.data, function(idx, res) {
                                seriesData.push([res.time, res.val]);
                                });

                                var seriesValues = {
                                        name: aData.name,
                                        data: seriesData,
                                        type: 'spline'
                               }

                            charts.addSeries(seriesValues, false);

                            charts.redraw();
                        });

1 个答案:

答案 0 :(得分:0)

我想以错误的方式将物品推入物体。

这可能是你的问题,为此:

$.each(data.y, function (index, value) {
    series.Real.data.push({
        y: value
    });
});

如果您在理解对象时遇到问题,那么使用上述[]表示法也可以写成:

series['Real']['data'].push(...);