Home  >  Article  >  Web Front-end  >  Use javascript to download json data in csv format_javascript skills

Use javascript to download json data in csv format_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:21:191420browse

Summary:
Recently, there is a small non-project requirement, which is to document the division of labor in project development to facilitate later management and maintenance. However, during development, the division of labor arrangements were recorded in json format, so I made a download of the json data to the local in csv format.

Code:

Copy code The code is as follows:



download csv

             


          

                                                                                                                                 

Enter JSON data


                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                       




download.js

Copy code

The code is as follows:

$(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 'rn';
            }
            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 'rn';
            }
            if (CSV == '') {
                growl.error("Invalid data");
                return;
            }
            var fileName = "Result";
            if (mo.msieversion()) {
                var IEwindow = window.open();
                IEwindow.document.write('sep=,rn' 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中定义的变量fileName。主要问题是他会自动添加一行空行,且每个元素都会在值前面加个'='。

下载下来的数据格式为:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn