开发者

Trying to push data to a JSON array

I'm trying create a gantt chart with highcharts and I need to put the data in the JSON format. I'm getting real close, but the problem i'm having is that the data I am pushing is being surrounded by quotes. I'm guessing there is just something that I am doing wrong, but I can't figure it out.

I can tell that the issue is with quotes being added because i have some static data that works just fine and I printed the object in the firebug console for the static data and my dynamic data.

So here is the very basic of my options 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)
        } ]
    }
}

Then I have this function which gets called at load time:

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);
    }
}

And here is the firebug output where I can see that the quotes are causing an issue for 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开发者_Go百科,05)", "y : Date.UTC(2010,12,23)}"]}]


shouldn't your start and end variables be object literals instead of strings?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜