有誰知道如何獲取csv url文件並將其轉換爲json對象,以便我可以在js中使用Google圖表工具?將csv文件轉換爲json對象數據表
回答
就我所知,對於大多數情況下,您可以將csv轉換爲js陣列的數組,矩陣或任何遵循圖表工具約定的javascript對象。
您可能需要:
- 獲取CSV文件內容
- Parse it
- 裹2的結果到您的圖表工具JSON
- 打電話給你的圖表庫
對於1,如果CSV文件託管在您的域中,則可以執行簡單的XMLHttpRequest,否則請嘗試在此搜索「s ame產地政策「。
棘手的部分是第2點。我已經看到幾個手工解析CSV文件失敗的嘗試(分號可以包含爲值的一部分,等等)...檢查出鏈接。
CSV和JSON是不同的格式。 JSON是分層結構,而CSV代表值的列表。所以我想你需要先解析CSV(當然是using a parser,當然是not implementing yourself)。這個解析器會給你一個對象,然後你可以將它序列化成JSON,或者在serializing(再次使用parser)之前轉換成另一個對象,以獲得所需的格式。
我意識到這是一個老問題,但今天我碰到它需要做同樣的事情,並寫了一個腳本來做到這一點。你可以在我的github repo查看。
下面的代碼將完成你(使用jQuery)後在做什麼:
$.ajax("http://my.domain.com/mycsvfile.csv", {
success: function(data) {
var jsonobject = csvjson.csv2json(data);
// Now use jsonobject to do some charting...
},
error: function() {
// Show some error message, couldn't get the CSV file
}
});
編碼愉快:)
我剛剛使用你的文件來獲得一些東西的快速原型。 ALl看起來很好,直到我發現\ r角色正在找到他們進入Mongo的路(我之前沒有注意到)。代碼中的幾點需要myString.replace(/ \ r/gm,「」)來清除它。 – joevallender 2012-06-13 15:50:02
@joevallender謝謝你指出。你有沒有機會記得你所要做的事情,並且可以爲它創造一個問題?https://github.com/aaronsnoswell/csvjson.js/issues/new。 – aaronsnoswell 2012-06-14 10:09:48
如果我在飛行中沒有執行提交請求,我會提交一個請求,現在我會將一些註釋放入問題隊列中 – joevallender 2012-06-14 13:57:47
使用此代碼爲指導來解析csv文件,以JSON ...
function processFiles(files) {
var file = files[0];
var reader = new FileReader();
reader.onload = function (e) {
var output = document.getElementById("fileOutput");
var texto = e.target.result;
csvJSON(texto);
};
reader.readAsText(file);
}
function csvJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers;
for (var i = 0; i < lines.length; i++) {
headers = lines[i].split("\n");
}
var cont = 0;
for (var i = 0; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split("\n");
for (var j = 0; j < headers.length; j++) {
obj[cont] = currentline[j];
}
cont++;
result.push(obj);
}
return JSON.stringify(result); //JSON
}
Papa Parse對此很好。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="libraries/PapaParse-4.1.2/papaparse.min.js"></script>
<script>
$(document).ready(function(){
$("#submitbutton").click(function(){
var myfile = $("#csvfile")[0].files[0];
var json = Papa.parse(myfile,
{
header: true,
skipEmptyLines: true,
complete: function(results) {
console.log("Dataframe:", JSON.stringify(results.data));
console.log("Column names:", results.meta.fields);
console.log("Errors:", results.errors);
}
});
})
})
</script>
</head>
<body>
<form name="foo" method="post" enctype="multipart/form-data">
<input id="csvfile" type="file" value="i">
</form>
<button id="submitbutton" type="button">Upload CSV file!</button>
</body>
</html>
載此CSV:
+------+----------------+---------------+------------+
| Id | Petal.Length | Petal.Width | Species |
+======+================+===============+============+
| 1 | 1.4 | 0.2 | setosa |
+------+----------------+---------------+------------+
| 2 | 1.4 | 0.2 | setosa |
+------+----------------+---------------+------------+
| 3 | 1.3 | 0.2 | setosa |
+------+----------------+---------------+------------+
| 4 | 3.9 | 1.4 | versicolor |
+------+----------------+---------------+------------+
| 5 | 3.5 | 1 | versicolor |
+------+----------------+---------------+------------+
| 6 | 4.2 | 1.5 | versicolor |
+------+----------------+---------------+------------+
| 7 | 5.4 | 2.3 | virginica |
+------+----------------+---------------+------------+
| 8 | 5.1 | 1.8 | virginica |
+------+----------------+---------------+------------+
你會得到在控制檯輸出:
Dataframe: [{"Id":"1","Petal.Length":"1.4","Petal.Width":"0.2","Species":"setosa"},{"Id":"2","Petal.Length":"1.4","Petal.Width":"0.2","Species":"setosa"},{"Id":"3","Petal.Length":"1.3","Petal.Width":"0.2","Species":"setosa"},{"Id":"4","Petal.Length":"3.9","Petal.Width":"1.4","Species":"versicolor"},{"Id":"5","Petal.Length":"3.5","Petal.Width":"1","Species":"versicolor"},{"Id":"6","Petal.Length":"4.2","Petal.Width":"1.5","Species":"versicolor"},{"Id":"7","Petal.Length":"5.4","Petal.Width":"2.3","Species":"virginica"},{"Id":"8","Petal.Length":"5.1","Petal.Width":"1.8","Species":"virginica"}]
Column names: ["Id", "Petal.Length", "Petal.Width", "Species"]
Errors: []
你試圖解析或CSV在JS另一種語言,只是有爲JS輸出JSON? – 2010-08-30 18:48:30
我能夠在後端的C#中將csv文件解析爲兩個數組。現在我想創建JSON對象(我可以在前端使用這兩個數組的數據)來綁定到Google js api圖表。 – locoboy 2010-08-30 19:39:39