将JSON值插入Javascript

时间:2019-05-05 18:15:22

标签: javascript arrays json

我有JSON这样的结果:

[{"Januari":"0","Februari":"0","Maret":"0","April":"0","Mei":"7","Juni":"0","Juli":"0","Agustus":"0","September":"0","Oktober":"0","November":"0","Desember":"0"}]

如何将JSON的值插入JavaScript对象中?

var ctx = document.getElementById("myAreaChart");
var myLineChart = new Chart(ctx, {
    type: 'line',
    data: {
        datasets: [{
           label: "Visitor",
           data: [0,0,0,0,7,0,0,0,0,0,0,0],  //<-- insert values here
    }],
},

1 个答案:

答案 0 :(得分:0)

使用Object.valuesArray.map可以提取值并将其解析为int,如下所示:

var json = [{"Januari":"0","Februari":"0","Maret":"0","April":"0","Mei":"7","Juni":"0","Juli":"0","Agustus":"0","September":"0","Oktober":"0","November":"0","Desember":"0"}];
var numbers = Object.values(json[0]).map(s => parseInt(s, 10));

最后更改您的示例:

data: numbers,