1

首先,我看到了 The Other Threads,我认为它们都不是使用 PHP 或 javascript。

好吧,在我编码并添加斜杠以转义 JSON 非法字符后,我有了这个数组:

"#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12" "#Tahrir","2012-Oct-12"  "#Tahrir","2012-Oct-19" "#Tahrir","2012-Oct-19" "#Tahrir","2012-Oct-19"

想要将其更改为 CSV 或找到一种方法将此示例与该图一起使用:http: //bl.ocks.org/3883245#data.tsv

提前致谢。

4

1 回答 1

0

要将 json 数据转换为 CSV,请尝试使用此http://jsfiddle.net/sturtevant/vUnF9/(不是我的代码)

function JSON2CSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

var str = '';
var line = '';

if ($("#labels").is(':checked')) {
    var head = array[0];
    if ($("#quote").is(':checked')) {
        for (var index in array[0]) {
            var value = index + "";
            line += '"' + value.replace(/"/g, '""') + '",';
        }
    } else {
        for (var index in array[0]) {
            line += index + ',';
        }
    }

    line = line.slice(0, -1);
    str += line + '\r\n';
}

for (var i = 0; i < array.length; i++) {
    var line = '';

    if ($("#quote").is(':checked')) {
        for (var index in array[i]) {
            var value = array[i][index] + "";
            line += '"' + value.replace(/"/g, '""') + '",';
        }
    } else {
        for (var index in array[i]) {
            line += array[i][index] + ',';
        }
    }

    line = line.slice(0, -1);
    str += line + '\r\n';
}
return str;
}
于 2013-12-18T22:05:30.157 回答