如何使用alasql

时间:2016-05-30 22:13:30

标签: javascript jquery json excel alasql

我有一个这样的json(可能有一些语法错误,我确实提取并重写了一些私有数据)

"todo-items": [{
    "id": 3511710,
    "company-name": "company1",
    "author" : "Jon Doe",        
    "tags": [{
        "id": 10101,
        "name": "2.Marketing Sales",
        "color": "#f78234"
    }],
    "creation-date": 20160101
}, {
    "id": 3511474,
    "company-name": "company1",
    "author" : "Jon Don",  
    "tags": [{
        "id": 10101,
        "name": "2.Marketing Sales",
        "color": "#f78234"
    }, {
        "id": 10103,
        "name": "4.Automotive",
        "color": "#9b7cdb"
    }],
    "creation-date": 20160101
 }, {
    "id": 3511478,
    "company-name": "company1",
    "author" : "Peter Jon Doe", 
    "tags": [{
        "id": 10101,
        "name": "2.Marketing Sales",
        "color": "#f78234"
    }, {
        "id": 9706,
        "name": "3.sales",
        "color": "#37ced0"
    }, {
        "id": 9562,
        "name": "3.IT",
        "color": "#37ced0"
    }],
    "creation-date": 20160101       
}]

我使用带有xlsx.core库的alasql将其导出到excel文件。 首先,我从原始json中删除STATUS(由团队API提供)然后我将JSON更改为javascript数组

$.ajax({
                type: "GET",
                url: requrl,
                headers: {"Authorization": "BASIC " + window.btoa(key + ":xxx")},
                success: function(data) {
                    delete data.STATUS;

                    //alert(data);
                   //console.log(data);
                  var arr = $.map(data, function(el) { return el });
                   //console.log(arr);
                   alasql("SELECT * INTO XLSX('test.xlsx',{headers:true}) FROM ? ",[arr]);

                   },
                error: function(response) {
                    alert(response);
                }
             });

这很好用,我可以将我的json提取为excel文件 但我在这个json中有标签对象的问题..当我将它更改为数组然后将其保存在excel中时,所有列都可以,除了标签,我只看到[对象] [对象] 你能帮我解决一下如何将这些标签变成正确的数组吗?最好的是一个标签= excel中的一列

编辑: 这是期望的输出 - 最多JSON中的标签数量将定义列数标签(无论是标签标签还是标签tag2 tags3都无关紧要) tags

0 个答案:

没有答案