Javascript 表中没有列。Json数据

Javascript 表中没有列。Json数据,javascript,json,google-visualization,Javascript,Json,Google Visualization,我有一个这种格式的json数据 [ { id: 2, nom: "Dell", type: "Pc", quantity: 78, prix: 7800 }, { id: 3, nom: "Intel", type: "Processor", quantity: 45, prix: 8000 }, { id: 4, nom: "Dell", type: "Ecran", quantity: 10, prix: 2500 } ] 我想把它们放在一个图表帮助中,请它大约是4天,我不能让它工作我想

我有一个这种格式的json数据

[
{
id: 2,
nom: "Dell",
type: "Pc",
quantity: 78,
prix: 7800
},
{
id: 3,
nom: "Intel",
type: "Processor",
quantity: 45,
prix: 8000
},
{
id: 4,
nom: "Dell",
type: "Ecran",
quantity: 10,
prix: 2500
}
] 
我想把它们放在一个图表帮助中,请它大约是4天,我不能让它工作我想了解为什么谷歌图表api不想接受这些数据这是我的代码

<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript"  src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the bar package.
google.charts.load('current', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var jsonData = $.ajax({
      url: "http://localhost:8080/get",
      dataType: "json",
      async: false
      }).responseText;
      console.log(jsonData)
  // Create our data table out of JSON data loaded from server.


  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new    google.visualization.ColumnChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});
}

</script>
</head>

<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>

//加载可视化API和bar包。
load('current',{'packages':['corechart']});
//将回调设置为在加载Google Visualization API时运行。
google.charts.setOnLoadCallback(drawChart);
函数绘图图(){
var jsonData=$.ajax({
url:“http://localhost:8080/get",
数据类型:“json”,
异步:false
}).responseText;
console.log(jsonData)
//使用从服务器加载的JSON数据创建我们的数据表。
var data=新的google.visualization.DataTable(jsonData);
//实例化并绘制图表,传入一些选项。
var chart=new google.visualization.ColumnChart(document.getElementById('chart_div'));
绘制图表(数据,{宽度:400,高度:240});
}

数据的格式必须如下所示:

{
    cols: [
        {id: 'task', label: 'Task', type: 'string'},
        {id: 'hours', label: 'Hours per Day', type: 'number'}
    ],
    rows: [
        {c:[{v: 'Work'}, {v: 11}]},
        {c:[{v: 'Eat'}, {v: 2}]},
        {c:[{v: 'Commute'}, {v: 2}]},
        {c:[{v: 'Watch TV'}, {v:2}]},
        {c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}
    ]
}
查看一下文档(),您将看到必须在cols属性中定义列,并在rows属性中定义数据

因此,您必须“重新格式化”JSON以满足需求

因此,为了满足您的转换需求,此代码应该可以工作:

var rows = new Array();

for(var i=0;i<jsonData.length;i++)
{
    var jsonRow = jsonData[i];
    var row = {
        c: [
            {v: jsonRow.id},
            {v: jsonRow.nom},
            {v: jsonRow.type},
            {v: jsonRow.quantity},
            {v: jsonRow.prix},
        ]
    };
    rows.push(row);
}


var dataFormatted = {
    cols: [
        { id: 'id', label: 'Id' },
        { id: 'nom', label: 'Nom' },
        { id: 'type', label: 'Type' },
        { id: 'quantity', label: 'Quantity' },
        { id: 'prix', label: 'Prix' },
    ],
    rows: rows
}

var data = new google.visualization.DataTable(dataFormatted);
var rows=new Array();

对于(var i=0;i如何转换?:”(编辑生成JSON的代码,使其以正确的格式生成。我能用javascript转换吗?我在后端使用spring boot,我不认为我会知道如何从那里编辑它们是的,你可以用JS来做。非常感谢你的帮助