尝试将数据推送到JSON数组

时间:2011-07-07 17:22:37

标签: javascript jquery json highcharts

我正在尝试使用highcharts创建一个甘特图,我需要将数据放在JSON格式中。我真的很接近,但我遇到的问题是我推动的数据被引号包围。我猜我只是做错了什么,但我无法弄明白。

我可以说问题是添加了引号,因为我有一些工作正常的静态数据,我在firebug控制台中打印了静态数据和动态数据的对象。

所以这是我的选项var:

的基础
var options = {
    series : [],
    test : [ {
        data : [ {
        low : Date.UTC(2012, 0, 1),
        y : Date.UTC(2012, 0, 15)
        }, {
        low : Date.UTC(2012, 0, 10),
        y : Date.UTC(2012, 4, 28)
        } ]
    }
}

然后我有这个函数在加载时被调用:

function loadData() {
    var chartData = $('#hiddenDate').val();
    console.log('hiddenDate = '+chartData);
    var goodData = chartData.split('|');
    console.log('goodData = '+goodData);

    var series = {
        data : []
    };

    try {
        $.each(goodData, function(index, value) {
            var goodData2 = value.split(",");
            var startYear = goodData2[0].substr(0, 4);
            var endYear = goodData2[1].substr(0, 4);
            var startMonth = goodData2[0].substr(5, 2);
            var endMonth = goodData2[1].substr(5, 2);
            var startDay = goodData2[0].substr(8, 2);
            var endDay = goodData2[1].substr(8, 2);
            /*series.data.push({
                low : 'Date.UTC('+startYear+','+startMonth+','+startDay+')',
                y : 'Date.UTC('+endYear+','+endMonth+','+endDay+')'
            });*/
            var start = "{low :  Date.UTC("+startYear+","+startMonth+","+startDay+")";
            var end = "y : Date.UTC("+endYear+","+endMonth+","+endDay+")}";
            series.data.push(start);
            series.data.push(end);
            //series.data.y.push('Date.UTC('+endYear+','+endMonth+','+endDay+')');
            console.log('series.data = '+series.data.toSource());
            console.log('options.test = '+options.test.toSource());
        });

        options.series.push(series);
        console.log('options.series = '+options.series.toSource());
    } catch (err) {
        console.log("ERROR ..." + err.description + '  message:'+ err.message);
    }
}

这是firebug输出,我可以看到引号引起options.series的问题:

series.data = ["{low : Date.UTC(2011,05,27)", "y : Date.UTC(2011,02,17)}", "{low : Date.UTC(2011,07,05)", "y : Date.UTC(2010,12,23)}"]
options.test = [{data:[{low:1325376000000, y:1326585600000}, {low:1326153600000, y:1338163200000}]}]
options.series = [{data:["{low : Date.UTC(2011,05,27)", "y : Date.UTC(2011,02,17)}", "{low : Date.UTC(2011,07,05)", "y : Date.UTC(2010,12,23)}"]}]

1 个答案:

答案 0 :(得分:2)

你的开始和结束变量不应该是对象文字而不是字符串吗?