如何动态地在javascript中使用json数据?

时间:2016-04-07 11:53:23

标签: javascript json getjson google-chartwrapper

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




  [
 {
 “日期”:“07-APR-16”,
 “总计”:6,
 “acceptedCount”:0,
 “submittedCount”:0,
 “rejectedCount”:null,
 “createdCount”:1
},
 {
 “日期”:“03-APR-16”,
 “总计”:2,
 “acceptedCount”:0,
 “submittedCount”:0,
 “rejectedCount”:null,
 “createdCount”:2
},
 {
 “日期”:“06-APR-16”,
 “总计”:13,
 “acceptedCount”:0,
 “submittedCount”:5,
 “rejectedCount”:null,
 “createdCount”:0
},
 {
 “日期”:“01-APR-16”,
 “总计”:20,
 “acceptedCount”:0,
 “submittedCount”:13,
 “rejectedCount”:null,
 “createdCount”:0
},
 {
 “日期”:“29-MAR-16”,
 “总计”:10,
 “acceptedCount”:0,
 “submittedCount”:8,
 “rejectedCount”:null,
 “createdCount”:0
},
 {
 “日期”:“04-APR-16”,
 “总计”:5,
 “acceptedCount”:0,
 “submittedCount”:3,
 “rejectedCount”:null,
 “createdCount”:0
},
 {
 “日期”:“31-MAR-16”,
 “总计”:30,
 “acceptedCount”:0,
 “submittedCount”:28,
 “rejectedCount”:null,
 “createdCount”:0
},
 {
 “日期”:“30-MAR-16”,
 “总计”: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]
]);
  

& #xA;

我应该如何在适当的行中映射json的每个对象??
任何人都可以帮助我吗?没有行可以动态增加.exapmle现在是4可能在一段时间后它可能变为8它依赖于json数据





我添加了我的html文件你能不能给我满解决方案。





 

 <!DOCTYPE html>
< html>
&#xA ;< HEAD>&#的xD;
 < title>付款服务的分析< / title>
< script type =“text / javascript”src =“https://www.google.com/jsapi”>< /脚本>&#的xD;
 < script type =“text / javascript”>
 google.load(“可视化”,“1”,{
包:[“corechart”,“bar”]
});
&#xA ; google.setOnLoadCallback(function(){
 drawChart()
 drawChart3()
 drawChart2()
 } && xD;



 function drawChart(){
&#xD ;
 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]
&#xA ;]);


 var options = {
图表:{
标题:'公司业绩',
副标题:'销售,费用和利润:2014-2017',
 },&#的xD;
 bars:'horizo​​ntal',// Material Bar Charts所需。
 hAxis:{
格式:'十进制'
 },&#的xD;
身高:500,
颜色:['#1b9e77','#d95f02','#7570b3','#db7f0b']
 };&#的xD;
&#的xD;
 var chart = new google.charts.Bar(document.getElementById('chart_div1'));

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

}

&#xA ;


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

 var options3 = {
标题:'公司业绩',
 curveType:'function',
传奇:{
位置:'底部'
 }&#的xD;
 };&#的xD;
&#的xD;
 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],&# xD;
 ['7-APRIL-16',1030,540]
]);

 var options2 = {
标题:'CreaditCard /发票明细',
 curveType:'function',
传奇:{
位置:'底部'
 }&#的xD;
 };&#的xD;
&#的xD;
 var chart2 = new google.visualization.LineChart(document.getElementById('cc / invoice'));

 chart2.draw(data2,options2);
}



 < /脚本>&#的xD;
&#的xD;
< /头>&#的xD;
<主体>&#的xD;
&#的xD; 
< 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})
&# XA;计数= $ {item.count}&#的xD;!
#端&#的xD;
< / BODY>&#的xD;
< / HTML>  
&#的xD;

&#的xD;

&#的xD;





4 个答案:

答案 0 :(得分:1)

循环json并将每个对象作为数组推送:

var array = []
array.push(['Day', 'Submitted', 'Accepted', 'Rejected', 'Created']);

for(var i = 0; i < json.length ; i++){

  arr = [json[i].date,json[i].total,json[i].acceptedCount,json[i].submittedCount,json[i].rejectedCount,json[i].createdCount];
  array.push(arr);
}

&#13;
&#13;
var 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
 }
]
var array = []
array.push(['Day', 'Submitted', 'Accepted', 'Rejected', 'Created']);

for(var i = 0; i < json.length ; i++){

    arr = [json[i].date,json[i].total,json[i].acceptedCount,json[i].submittedCount,json[i].rejectedCount,json[i].createdCount];
  array.push(arr);
}

document.getElementById('arraY').innerHTML = JSON.stringify(array,null,4)
&#13;
<pre id="arraY">
</pre>
&#13;
&#13;
&#13;

https://jsfiddle.net/37urrtLt/

编辑

谷歌图表上的

http://jsfiddle.net/srrrn9sa/282/示例,但我根本不了解谷歌图表,因此您可以使用它来解决您的问题

<强> EDIT2

http://jsfiddle.net/mg2qtato/1/;)

<强> EDIT3

Full example

答案 1 :(得分:1)

您必须使用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);

答案 2 :(得分:0)

只是为了映射,您可以使用此解决方案。

&#13;
&#13;
var array = [{ "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 }],
    newArray = function (array, titles, cols) {
        var r = [titles];
        array.forEach(function (a) {
            r.push(cols.map(function (b) {
                return a[b];
            }));
        })
        return r;
    }(array, ['Day', 'Submitted', 'Accepted', 'Rejected', 'Created'], ['date', 'submittedCount', 'acceptedCount', 'rejectedCount', 'createdCount'])


document.write('<pre>' + JSON.stringify(newArray, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 3 :(得分:0)

请运行此代码,其中变量'a'代表您的json数组:

var aTemp1 = ['Day','Submitted','Accepted','Rejected','Created'];
var aMain = [];
aMain.push(aTemp1);
console.log(aMain);
$.each(a, function(ind, val) {
  var _date = val.date;
  var _sub  = val.submittedCount;
  var _acc = val.acceptedCount;
  var _rej  = val.rejectedCount;
  var _cre  = val.createdCount;
  var aTemp  = [_date, _sub, _acc, _rej, _cre];
  aMain.push(aTemp);
  });
   console.log(aMain);