0

我想将 JSON 对象作为字符串放入 javascript 变量中以创建图形。

qm.createGraphData = function() {
$.post("ajax_getGraphDataWebsite    ", function(json) {
           qm.negativesData = json;
    },"json");  

    qm.data = [{
        "xScale":"ordinal",
        "comp":[],
        "main":[{
            "className":".main.l1",
            qm.negativesData},{
            "className":".main.l2",
            qm.negativesData}],
        "type":"line-dotted",
        "yScale":"linear"}];
}

字符串值应添加到“数据”部分。现在添加了对象,但我需要将字符串值添加到变量中,如下例所示:

{"data":[{"x":"3283581","y":"2013-10-16"},{"x":"1512116","y":"2013-10-17"},{"x":"3967","y":"2013-10-18"},{"x":"1094","y":"2013-10-19"},{"x":"853","y":"2013-10-20"},{"x":"1205","y":"2013-10-21"},{"x":"2618700","y":"2013-10-22"},{"x":"3928291","y":"2013-10-23"},{"x":"3670318","y":"2013-10-24"},{"x":"3347369","y":"2013-10-25"},{"x":"2525573","y":"2013-10-26"},{"x":"3224612","y":"2013-10-27"},{"x":"3992964","y":"2013-10-28"},{"x":"3949904","y":"2013-10-29"},{"x":"3568618","y":"2013-10-30"},{"x":"3104696","y":"2013-10-31"},{"x":"3246932","y":"2013-11-01"},{"x":"2817758","y":"2013-11-02"},{"x":"3198856","y":"2013-11-03"},{"x":"3952957","y":"2013-11-04"},{"x":"3934173","y":"2013-11-05"},{"x":"3878718","y":"2013-11-06"},{"x":"3642822","y":"2013-11-07"},{"x":"3186096","y":"2013-11-08"}]}

这将为我生成正确的图表。有谁知道如何将 json 对象转换为上面的字符串并将其发送到 qm.negativesData 变量?

// 更新

现在我得到了带有 qm.negativesData = JSON.stringify(json); 的字符串 解决方案

但是我的 qm.negativesdata 不会添加到 qm.data 变量中...我收到控制台错误 SyntaxError: invalid property id

我想我没有以正确的方式添加它们?

4

1 回答 1

1

要将 JSON 对象转换为 JSON 字符串,您可以尝试myObject.stringify(), JSON.stringify(myObject),或者如果您正在使用库,则使用该库的内置函数。

因此,您可以执行以下操作:qm.negativesData = myObject.stringify()

干杯

于 2013-11-08T21:18:41.027 回答