我看过一些将 json 转换为 csv 的帖子,例如“ http://jsfiddle.net/FLR4v/ ”。我无法让他们将此 json 转换为 csv 。
这是我的代码(这很好用,但不适用于下面注释掉的 var json3)
<html>
<head>
<title>JSON to CSV</title>
<script src="json.js" type="text/javascript"></script>
<script type="text/javascript">
//var json3 = { "inp1:val1": { "data": [ [ 1378267200000, 0.0743 ], [ 1378270800000, 0.1787 ] ] }}
var json3 = { "data": [ [ 1378267200000, 0.0743 ], [ 1378270800000, 0.1787 ], ] }
DownloadJSON2CSV(json3.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] + ',';
}
// Here is an example where you would wrap the values in double quotes
// 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>This page does nothing....</h1>
</body>
</html>
上面的代码工作正常。我需要的是上面需要与下面一起工作
var json3 = { "inp1:val1": { "data": [ [ 1378267200000, 0.0743 ], [ 1378270800000, 0.1787 ] ] }}