js將json資料以csv格式下載

來源:互聯網
上載者:User

標籤:

摘要:

  最近有一個非項目的小需求,就是將項目開發分工檔案化,方便後期管理維護。但是開發時,分工安排都是以json格式記錄的,所以就做了一個將json資料以csv格式下載到本地。

代碼:
<!DOCTYPE html><html>    <title>download csv</title>    <head>        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>        <script type="text/javascript" src="download.js"></script>    </head>    <body>        <div align="center">            <h3><u>Enter JSON data</u></h3>            <div class=‘mydiv‘>                    <textarea id="txt" class=‘txtarea‘ rows="15" cols="100">[{"Vehicle":"BMW","Date":"30 Jul 2013 09:24 AM","Location":"Hauz Khas","Speed":42},{"Vehicle":"Honda CBR","Date":"30 Jul 2013 12:00 AM","Location":"Military Road","Speed":0},{"Vehicle":"Supra","Date":"30 Jul 2013 07:53 AM","Location":"Sec-45","Speed":58},{"Vehicle":"Land Cruiser","Date":"30 Jul 2013 09:35 AM","Location":"DLF Phase I","Speed":83}]</textarea>            </div>            <br/>            <button class="download">Download CSV</button>        </div>    </body></html>

download.js

$(document).ready(function() {    "use strict";    var mo = {        init: function() {            $(‘.download‘).click(function() {                var data = $(‘#txt‘).val();                if (data === ‘‘) {                    return;                }                mo.JSONToCSVConvertor(data, true);            });        },        JSONToCSVConvertor: function(JSONData, ShowLabel) {            var arrData = typeof JSONData !== ‘object‘ ? JSON.parse(JSONData) : JSONData;            var CSV = ‘‘;            if (ShowLabel) {                var row = "";                for (var index in arrData[0]) {                    row += index + ‘,‘;                }                row = row.slice(0, -1);                CSV += row + ‘\r\n‘;            }            for (var i = 0; i < arrData.length; i++) {                var row = "";                for (var index in arrData[i]) {                    var arrValue = arrData[i][index] == null ? "" : ‘="‘ + arrData[i][index] + ‘"‘;                    row += arrValue + ‘,‘;                }                row.slice(0, row.length - 1);                CSV += row + ‘\r\n‘;            }            if (CSV == ‘‘) {                growl.error("Invalid data");                return;            }            var fileName = "Result";            if (mo.msieversion()) {                var IEwindow = window.open();                IEwindow.document.write(‘sep=,\r\n‘ + CSV);                IEwindow.document.close();                IEwindow.document.execCommand(‘SaveAs‘, true, fileName + ".csv");                IEwindow.close();            } else {                var uri = ‘data:application/csv;charset=utf-8,‘ + escape(CSV);                var link = document.createElement("a");                link.href = uri;                link.style = "visibility:hidden";                link.download = fileName + ".csv";                document.body.appendChild(link);                link.click();                document.body.removeChild(link);            }        },        msieversion: function() {            var ua = window.navigator.userAgent;            var msie = ua.indexOf("MSIE ");            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number             {                return true;            } else { // If another browser,                 return false;            }            return false;        },        main: function() {            mo.init();        }    };    mo.main();});

 

 

小結:

  注意json格式[{},{}],檔案名稱是在js中定義的變數fileNam。下載下來的資料格式為

 

js將json資料以csv格式下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.