简体   繁体   中英

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)

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.
Note: For simplicity, I have the same JSON information I receive as a string called arrayString :

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.

Is anyone able to assist me with resolving this?

References: Both of these examples use markLine:
http://echarts.baidu.com/echarts2/doc/example/line1.html#-en
http://echarts.baidu.com/echarts2/doc/example/bar13.html#-en

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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