简体   繁体   English

JavaScript数组JSON.parse(字符串)

[英]JavaScript Array JSON.parse (string)

I am using eCharts (a JavaScript charting library) and there is something that's doing my head in. The examples use the following code for markLine (and it works as expected) 我正在使用eCharts(一个JavaScript图表库),正在做一些事情。这些示例对markLine使用以下代码(并且按预期工作)

markLine : {
    symbol: 'none',
    tooltip: {show: false},
    itemStyle:{
        normal:{
            lineStyle:{
                type: 'solid',
                color: '#CCCCCC'
            },
            tooltip:{
                show: false
            }
        }
    },
    data: [ [{ "xAxis" : 250, "yAxis" : 0 }, {"xAxis": 250, "yAxis" : 250 }] ]
}

I need to be able to get the data part as a JSON string, but I cannot get it to work. 我需要能够将数据部分作为JSON字符串获取,但是我无法使其正常工作。
Note: For simplicity, I have the same JSON information I receive as a string called arrayString : 注意:为简单起见,我具有与称为arrayString的字符串相同的JSON信息

markLine : {
    symbol: 'none',
    tooltip: {show: false},
    itemStyle:{
        normal:{
            lineStyle:{
                type: 'solid',
                color: '#CCCCCC'
            },
            tooltip:{
                show: false
            }
        }
    },
    data: (function (){
        var res = [];
        var arrayString = "";

        arrayString = '[{ "xAxis" : 250, "yAxis" : 0 }, {"xAxis": 250, "yAxis" : 250 }]';
        res = JSON.parse(arrayString);

        return res;
    })()
}

When I run the JSON.parse code the chart doesn't display, but if I console.log the value of 'res' the array appears to be created correctly. 当我运行JSON.parse代码时,图表不会显示,但是如果我console.log,则'res'的值似乎已正确创建。

Is anyone able to assist me with resolving this? 有谁能协助我解决这个问题?

References: Both of these examples use markLine: 参考:这两个示例都使用markLine:
http://echarts.baidu.com/echarts2/doc/example/line1.html#-en http://echarts.baidu.com/echarts2/doc/example/line1.html#-en
http://echarts.baidu.com/echarts2/doc/example/bar13.html#-en http://echarts.baidu.com/echarts2/doc/example/bar13.html#-en

您应该添加到res数组而不是忽略它吗?

res.push(JSON.parse(arrayString));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM