我不确定您在做什么,但这会使用Javascript从JSON转换为CSV。这使用的是开源JSON库,因此只需将JSON.js下载到与您将下面的代码保存到的文件夹相同的文件夹中,它将静态JSON值解析
json3为CSV并提示您在Excel中下载/打开。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>JSON to CSV</title> <script src="scripts/json.js" type="text/javascript"></script> <script type="text/javascript"> var json3 = { "d": "[{"Id":1,"UserName":"Sam Smith"},{"Id":2,"UserName":"Fred Frankly"},{"Id":1,"UserName":"Zachary Zupers"}]" } DownloadJSON2CSV(json3.d); 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 + 'rn'; } window.open( "data:text/csv;charset=utf-8," + escape(str)) } </script></head><body> <h1>This page does nothing....</h1></body></html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)