如何从JSON数据修改输出到csv?

时间:2014-07-01 00:46:50

标签: javascript json csv

我找到了一个将JSON数据转换为CSV的示例代码,我尝试使用图形here来实现。这是代码:

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

    var CSV = '';    
    //Set Report title in first row or line

    CSV += ReportTitle + '\r\n\n';

    //This condition will generate the Label/Header
    if (ShowLabel) {
        var row = "";

        //This loop will extract the label from 1st index of on array
        for (var index in arrData[0]) {

            //Now convert each value to string and comma-seprated
            row += index + ',';
        }

        row = row.slice(0, -1);

        //append Label row with line break
        CSV += row + '\r\n';
    }

    //1st loop is to extract each row
    for (var i = 0; i < arrData.length; i++) {
        var row = "";

        //2nd loop will extract each column and convert it in string comma-seprated
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '"\n';
        }

        row.slice(0, row.length - 1);

        //add a line break after each row
        CSV += row + '\r\n';
    }

    if (CSV == '') {        
        alert("Invalid data");
        return;
    }   

    //Generate a file name
    var fileName = "DataOfficial_";
    //this will remove the blank-spaces from the title and replace it with an underscore
    fileName += ReportTitle.replace(/ /g,"_");   

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

    // Now the little tricky part.
    // you can use either>> window.open(uri);
    // but this will not work in some browsers
    // or you will not get the correct file extension    

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");    
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

但是在CSV文件中,这不是我想要的。该文件包含一行中的值,如下所示:

2012-06-26,1271000,1138683600000,1271000,1141102800000,1271000,1143781200000,0,1146369600000,0,1149048000000,0,1151640000000,0,1154318400000,0,1156996800000,0,1159588800000,3899486,1162270800000,3899486,1164862800000,3899486,1167541200000,3564700,1170219600000,3564700,1172638800000,3564700,1175313600000,2648493,1177905600000,2648493,1180584000000,2648493,1183176000000,2522993,1185854400000,2522993,1188532800000,2522993,1191124800000,2906501,1193803200000,2906501,1196398800000,2906501,1199077200000,2206761,1201755600000,2206761,1204261200000,2206761,1206936000000,2287726,1209528000000,2287726,1212206400000,2287726,1214798400000,2732646,1217476800000,2732646,1220155200000,2732646,1222747200000,2599196,1225425600000,2599196,1228021200000,2599196,1230699600000,1924387,1233378000000,1924387,1235797200000,1924387,1238472000000,1756311,1241064000000,1756311,1243742400000,1756311,1246334400000,1743470,1249012800000,1743470,1251691200000,1743470,1254283200000,1519010,1256961600000,1519010,1259557200000,1519010,1262235600000,1591444,1264914000000,1591444,1267333200000,1591444,1270008000000,1543784,1272600000000,1543784,1275278400000,1543784,1277870400000,1309915,1280548800000,1309915,1283227200000,1309915,1285819200000,1331875,1288497600000,1331875,1291093200000,1331875,1293771600000,1331875,1296450000000,1154695,1298869200000,1154695,1301544000000,1194025,1304136000000,1194025,1306814400000,1194025,1309406400000,1194025,1312084800000,1194025,1314763200000,1244525,1317355200000,475000,1320033600000,475000,1322629200000,475000,1325307600000,690033,1327986000000,690033,1330491600000,690033,1333166400000,514733,1335758400000,514733

我想以CSV格式获取数据here。我怎样才能做到这一点?

0 个答案:

没有答案