我如何解析这个json
[
{"CODE":"1","MONTH":"1","TOTAL":78},
{"CODE":"1","MONTH":"2","TOTAL":122},
{"CODE":"1","MONTH":"3","TOTAL":102},
{"CODE":"1","MONTH":"4","TOTAL":65},
{"CODE":"1","MONTH":"5","TOTAL":134},
{"CODE":"1","MONTH":"6","TOTAL":88},
{"CODE":"1","MONTH":"7","TOTAL":77},
{"CODE":"1","MONTH":"8","TOTAL":58},
{"CODE":"1","MONTH":"9","TOTAL":67},
{"CODE":"1","MONTH":"10","TOTAL":69},
{"CODE":"2","MONTH":"5","TOTAL":4},
{"CODE":"2","MONTH":"6","TOTAL":87},
{"CODE":"2","MONTH":"7","TOTAL":81},
{"CODE":"2","MONTH":"8","TOTAL":105},
{"CODE":"2","MONTH":"9","TOTAL":112},
{"CODE":"2","MONTH":"10","TOTAL":85},
]
采用此格式
series: [{
name: '1',
data: [78, 122, 102, 65, 134, 88, 77, 58, 67, 69, 0, 0]
}, {
name: '2',
data: [0, 0, 0, 0, 4, 87, 81, 105, 112, 85, 0, 0]
}]
有效地使用javascript或jquery函数吗? 在Json中,每行代表每个代码(1或2)每月的总值
对于CODE = 1,我的总数只有十个月(第1个月= 1月,第2个月= feb ...月10 = 10月)
对于CODE = 2,我在may和octuber之间的总计只有五个月。
在这个数组中[78,122,102,65,134,88,77,58,67,69,0,0] 78是1月的总数,2月的122,行军的102 ...
对于那些我没有总数的月份,例如十一月或十二月,我需要放一个0
我的json将返回更多信息,更多代码我无法使用暴力
提前致谢 JC
答案 0 :(得分:1)
您可以运行如下循环:
var temp = [];
for (var i =0; i< data.length;i++) {
var obj = data[i],
dup = false;
for (var j = 0; j < temp.length; j++) {
var item = temp[j];
if (obj.CODE == item.name) {
item.data.push(obj.TOTAL);
dup = true;
break;
}
}
if (!dup) {
temp.push({
name: obj.CODE,
data: [obj.TOTAL]
});
}
}
var data = [{
"CODE": "1",
"MONTH": "1",
"TOTAL": 78
}, {
"CODE": "1",
"MONTH": "2",
"TOTAL": 122
}, {
"CODE": "1",
"MONTH": "3",
"TOTAL": 102
}, {
"CODE": "1",
"MONTH": "4",
"TOTAL": 65
}, {
"CODE": "1",
"MONTH": "5",
"TOTAL": 134
}, {
"CODE": "1",
"MONTH": "6",
"TOTAL": 88
}, {
"CODE": "1",
"MONTH": "7",
"TOTAL": 77
}, {
"CODE": "1",
"MONTH": "8",
"TOTAL": 58
}, {
"CODE": "1",
"MONTH": "9",
"TOTAL": 67
}, {
"CODE": "1",
"MONTH": "10",
"TOTAL": 69
},
{
"CODE": "2",
"MONTH": "5",
"TOTAL": 4
}, {
"CODE": "2",
"MONTH": "6",
"TOTAL": 87
}, {
"CODE": "2",
"MONTH": "7",
"TOTAL": 81
}, {
"CODE": "2",
"MONTH": "8",
"TOTAL": 105
}, {
"CODE": "2",
"MONTH": "9",
"TOTAL": 112
}, {
"CODE": "2",
"MONTH": "10",
"TOTAL": 85
}
];
var temp = [];
for (var i = 0; i < data.length; i++) {
var obj = data[i],
dup = false;
for (var j = 0; j < temp.length; j++) {
var item = temp[j];
if (obj.CODE == item.name) {
item.data.push(obj.TOTAL);
dup = true;
break;
}
}
if (!dup) {
temp.push({
name: obj.CODE,
data: [obj.TOTAL]
});
}
}
console.log(temp);
&#13;
<p>Check browser console <sup>F12</sup> to see the result<p>
&#13;
答案 1 :(得分:0)
这是一个蛮力的例子,只做了所要求的。
var originalData = [
{"CODE":"1","MONTH":"1","TOTAL":78},
{"CODE":"1","MONTH":"2","TOTAL":122},
{"CODE":"1","MONTH":"3","TOTAL":102},
{"CODE":"1","MONTH":"4","TOTAL":65},
{"CODE":"1","MONTH":"5","TOTAL":134},
{"CODE":"1","MONTH":"6","TOTAL":88},
{"CODE":"1","MONTH":"7","TOTAL":77},
{"CODE":"1","MONTH":"8","TOTAL":58},
{"CODE":"1","MONTH":"9","TOTAL":67},
{"CODE":"1","MONTH":"10","TOTAL":69},
{"CODE":"2","MONTH":"5","TOTAL":4},
{"CODE":"2","MONTH":"6","TOTAL":87},
{"CODE":"2","MONTH":"7","TOTAL":81},
{"CODE":"2","MONTH":"8","TOTAL":105},
{"CODE":"2","MONTH":"9","TOTAL":112},
{"CODE":"2","MONTH":"10","TOTAL":85},
];
var newData={};
var tempData1=[];
var tempData2=[];
for (var ii=0;ii<originalData.length;ii++){
if (originalData[ii].CODE==="1"){
tempData1.push(originalData[ii].TOTAL);
}
else{
tempData2.push(originalData[ii].TOTAL);
}
}
newData['series'] =[ {name: '1',
data: tempData1},
{
name: '2',
data: tempData2
}];
console.log(JSON.stringify(newData));
答案 2 :(得分:0)
var oData = [
{"CODE":"1","MONTH":"1","TOTAL":78},
{"CODE":"1","MONTH":"2","TOTAL":122},
{"CODE":"1","MONTH":"3","TOTAL":102},
{"CODE":"1","MONTH":"4","TOTAL":65},
{"CODE":"1","MONTH":"5","TOTAL":134},
{"CODE":"1","MONTH":"6","TOTAL":88},
{"CODE":"1","MONTH":"7","TOTAL":77},
{"CODE":"1","MONTH":"8","TOTAL":58},
{"CODE":"1","MONTH":"9","TOTAL":67},
{"CODE":"1","MONTH":"10","TOTAL":69},
{"CODE":"2","MONTH":"5","TOTAL":4},
{"CODE":"2","MONTH":"6","TOTAL":87},
{"CODE":"2","MONTH":"7","TOTAL":81},
{"CODE":"2","MONTH":"8","TOTAL":105},
{"CODE":"2","MONTH":"9","TOTAL":112},
{"CODE":"2","MONTH":"10","TOTAL":85},
],
nData = [{ name: '1', data: [] }, { name: '2', data: [] }];
for (var n = 1; n <= 12; n++) {
var d1 = oData.filter(function(d) {
return d.CODE == '1' && d.MONTH == n;
}),
d2 = oData.filter(function(d) {
return d.CODE == '2' && d.MONTH == n;
});
nData[0].data.push(d1.length ? d1[0].TOTAL : 0);
nData[1].data.push(d2.length ? d2[0].TOTAL : 0);
}
$('pre.out').text( JSON.stringify(nData) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre class="out"></pre>