gpt4 book ai didi

javascript - 如何在javascript中动态操作json数据?

转载 作者:行者123 更新时间:2023-11-29 21:31:04 25 4
gpt4 key购买 nike

以下是我点击 url 时的 json 数据。

[
{
"date": "07-APR-16",
"total": 6,
"acceptedCount": 0,
"submittedCount": 0,
"rejectedCount": null,
"createdCount": 1
},
{
"date": "03-APR-16",
"total": 2,
"acceptedCount": 0,
"submittedCount": 0,
"rejectedCount": null,
"createdCount": 2
},
{
"date": "06-APR-16",
"total": 13,
"acceptedCount": 0,
"submittedCount": 5,
"rejectedCount": null,
"createdCount": 0
},
{
"date": "01-APR-16",
"total": 20,
"acceptedCount": 0,
"submittedCount": 13,
"rejectedCount": null,
"createdCount": 0
},
{
"date": "29-MAR-16",
"total": 10,
"acceptedCount": 0,
"submittedCount": 8,
"rejectedCount": null,
"createdCount": 0
},
{
"date": "04-APR-16",
"total": 5,
"acceptedCount": 0,
"submittedCount": 3,
"rejectedCount": null,
"createdCount": 0
},
{
"date": "31-MAR-16",
"total": 30,
"acceptedCount": 0,
"submittedCount": 28,
"rejectedCount": null,
"createdCount": 0
},
{
"date": "30-MAR-16",
"total": 5,
"acceptedCount": 0,
"submittedCount": 4,
"rejectedCount": null,
"createdCount": 0
}
]

以下是我的javascript的一部分

  var data = google.visualization.arrayToDataTable([
['Day', 'Submitted', 'Accepted', 'Rejected', 'Created'],
['29-MAR-16', 100, 410, 230, 40],
['30-MAR-16', 170, 346, 302, 430],
['31-MAR-16', 60, 100, 130, 40],
['1-APRIL-16', 302, 350, 520, 40]
]);

我应该如何将 json 的每个对象映射到适当的行中??任何人都可以帮助我吗?没有行可能会动态增加。exapmle 现在它是 4 也许在某个时候它可能会变成 8 它取决于 json 数据

我添加了我的 html 文件,你能给我完整的解决方案吗?

<!DOCTYPE html>
<html>
<head>
<title>Analytics of Payment Service</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {
packages: ["corechart", "bar"]
});
google.setOnLoadCallback(function(){
drawChart()
drawChart3()
drawChart2()
})



function drawChart() {

var data = google.visualization.arrayToDataTable([
['Day', 'Submitted', 'Published', 'Rejected', 'Created'],
['29-MAR-16', 100, 410, 230, 40],
['30-MAR-16', 170, 346, 302, 430],
['31-MAR-16', 60, 100, 130, 40],
['1-APRIL-16', 302, 350, 520, 40]
]);


var options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
bars: 'horizontal', // Required for Material Bar Charts.
hAxis: {
format: 'decimal'
},
height: 500,
colors: ['#1b9e77', '#d95f02', '#7570b3', '#db7f0b']
};

var chart = new google.charts.Bar(document.getElementById('chart_div1'));

chart.draw(data, google.charts.Bar.convertOptions(options));

}




function drawChart3() {
var data3 = google.visualization.arrayToDataTable([
['Day', 'Sales'],
['4-APRIL-16', 1000],
['5-APRIL-16', 1170],
['6-APRIL-16', 660],
['7-APRIL-16', 1030]
]);

var options3 = {
title: 'Company Performance',
curveType: 'function',
legend: {
position: 'bottom'
}
};

var chart3 = new google.visualization.LineChart(document.getElementById('curve_chart'));

chart3.draw(data3, options3);
}



function drawChart2() {
var data2 = google.visualization.arrayToDataTable([
['Day', 'CreaditCard', 'Invoice'],
['4-APRIL-16', 1000, 400],
['5-APRIL-16', 1170, 460],
['6-APRIL-16', 660, 1120],
['7-APRIL-16', 1030, 540]
]);

var options2 = {
title: 'CreaditCard/Invoice Details',
curveType: 'function',
legend: {
position: 'bottom'
}
};

var chart2 = new google.visualization.LineChart(document.getElementById('cc/invoice'));

chart2.draw(data2, options2);
}



</script>

</head>
<body>

<div id="chart_div1" style="width: 900px; height: 500px;"></div>

<div id="curve_chart" style="width: 900px; height: 500px"></div>

<div id="cc/invoice" style="width: 900px; height: 500px"></div>

#foreach($item in ${chartBarForSubmittedManid})
count=$!{item.count}
#end
</body>
</html>

最佳答案

您必须使用 jQuery、JSON 等解析 JSON 响应数据(由 URL 返回的数据)。

var listOfData = $.parseJSON(responsedata); //jQuery
var listOfData = JSON.parse(responsedata); // JSON

listOfData 将是一个包含对象的数组。您需要循环进入它以构建您的目标数组输出。

var outputArray = new Array();
// header
var headerArray = ['Day', 'Submitted', 'Accepted', 'Rejected', 'Created'];
outputArray.push(headerArray);
for (var i = 0; i < listOfData.length; i++){
var obj = listOfData[i];
var innerArray = new Array();
innerArray[0] = obj.date;
innerArray[1] = obj.submittedCount;
innerArray[2] = obj.acceptedCount;
innerArray[3] = obj.rejectedCount;
innerArray[4] = obj.createdCount;
outputArray.push(innerArray);
}

现在然后使用您代码这一行的输出

var data = google.visualization.arrayToDataTable(outputArray);

关于javascript - 如何在javascript中动态操作json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36475396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com