好的,我终于使这段代码起作用了:
<html><head> <title>Demo - Covnert JSON to CSV</title> <script type="text/javascript" src="http://pre.jquery.com/jquery-latest.js"></script> <script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script> <script type="text/javascript"> // JSON to CSV Converter function ConvertToCSV(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]) { if (line != '') line += ',' line += array[i][index]; } str += line + 'rn'; } return str; } // Example $(document).ready(function () { // Create Object var items = [ { name: "Item 1", color: "Green", size: "X-Large" }, { name: "Item 2", color: "Green", size: "X-Large" }, { name: "Item 3", color: "Green", size: "X-Large" }]; // Convert Object to JSON var jsonObject = JSON.stringify(items); // Display JSON $('#json').text(jsonObject); // Convert JSON to CSV & Display CSV $('#csv').text(ConvertToCSV(jsonObject)); }); </script></head><body> <h1> JSON</h1> <pre id="json"></pre> <h1> CSV</h1> <pre id="csv"></pre></body></html>
非常感谢您对所有贡献者的支持。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)