下面的代码运行良好。我是ajax的新手。需要将像' http://test.com/test.php '这样的url调用的输出分配给第6行的变量json3。url调用的输出看起来像{“inp1:val1”:{“data ": [ [ 1378267200000, 0.0743 ], [ 1378270800000, 0.1787 ] }}
目前我有这样的硬编码 var json3 = { "inp1:val1": { "data": [ [ 1378267200000, 0.0743 ], [ 1378270800000, 0.1787 ] }}
<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 ] ] }}
DownloadJSON2CSV(json3["inp1:val1"].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>This page downloads csv....</h1>
</body>
</html>
谢谢你的帮助