我下面的代码在另一个下面输出 csv 文件,例如
日期、值
inp2:val2
09172013, 1
08172013, 2
inp3:val3
09172013, 10.1
08172013, 20
这里第一列是日期,第二列是值
我怎样才能让它显示为 Date inp2 inp3
09172013, 1 , 10.1
08172013, 2 , 20
谢谢
编码
<html">
<head>
<title>JSON to CSV</title>
<script src="json.js" type="text/javascript"></script>
<script type="text/javascript">
var str = '';
var json3 =
{"inp2:val2": {"data": [[09172013, 1], [08172013, 2]]}, "inp3:val3": {"data": [[09172013, 10.1], [08172013, 20.0]]}}
str +='Date,Value'+ '\r\n';
str +='inp2:val2,'+ '\r\n';
DownloadJSON2CSV(json3["inp2:val2"].data);
str +='inp3:val3,'+ '\r\n';
DownloadJSON2CSV(json3["inp3:val3"].data);
function DownloadJSON2CSV(objArray)
{
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
// var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
line += array[i][index] + ',';
}
line.slice(0,line.Length-1);
str += line + '\r\n';
}
}
window.open( "data:text/csv;charset=utf-8," + escape(str))
</script>
</head>
<body>
<h1>See the downloaded csv file ....</h1>
</body>
</html>