尝试将数据推送到JSON数组 [英] Trying to push data to a JSON array

查看:67
本文介绍了尝试将数据推送到JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 highcharts 创建一个甘特图,我需要将数据以JSON格式放置.我已经很接近了,但是我遇到的问题是我要推送的数据被引号包围了.我猜只是我做错了什么,但我无法弄清楚.

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.

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

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.

因此,这是我的var选项的最基本内容:

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

这是萤火虫的输出,在这里我可以看到引号引起options.series的问题:

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,05)", "y : Date.UTC(2010,12,23)}"]}]

推荐答案

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

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

这篇关于尝试将数据推送到JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆