[英]How convert tsv to Json
I want to make a dynamic graph based on a json file. 我想基于json文件制作动态图。 I have seen many examples with tsv but I donot how to convert it to json.
我已经见过很多关于tsv的例子,但是我不知道如何将其转换为json。 That is the part that I want to change from tsv to json but I donot know how!
这是我想从tsv更改为json的部分,但我不知道如何!
d3.tsv("data/data.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
when I use 当我使用
d3.json("data/data.json", function(data) {
data.forEach(function d) {
d.date = parseDate(d.date);
d.close = +d.close;
}
});
it gives this error: Uncaught type error: cannot call method 'forEach' of undefined! 它给出以下错误:未捕获的类型错误:无法调用未定义的方法'forEach'!
Thanks for your suggestions :) 感谢您的建议:)
try to do something like this 尝试做这样的事情
d3.json("data/data.json", function(data) {
data.forEach(function d) {
d.date = parseDate(d.date);
d.close = +d.close;
}
});
d3.js have support for json, https://github.com/mbostock/d3/wiki/Requests d3.js支持json, https://github.com/mbostock/d3/wiki/Requests
The syntax around your forEach
is a little off; forEach
周围的语法有点差; try this instead: 试试这个代替:
d3.json("data/data.json", function(data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
});
(As Felix points out, this will only work if your JSON object is defined and is an array) (正如Felix指出的那样,这仅在定义了JSON对象且为数组的情况下才有效)
Here a small code where you'll be able to convert tsv to json. 这是一个小代码,您可以在其中将tsv转换为json。 It could help you...
它可以帮助您...
ps : here is typescript, but you can easily convert it to vanilla javascript ;) ps:这是打字稿,但您可以轻松将其转换为原始javascript;)
// Set bunch of datas into format object
tsvToJson(datas: string): Array<Object>{
// Separate each lines
let array_datas = datas.split(/\r\n|\r|\n/g);
// Separate each values into each lines
var detailed_datas = [];
for(var i = 0; i < array_datas.length; i++){
detailed_datas.push(array_datas[i].split("\t"));
}
// Create index
var index = [];
var last_index = ""; // If the index we're reading is equal to "", it mean it might be an array so we take the last index
for(var i = 0; i < detailed_datas[0].length; i++){
if(detailed_datas[0][i] == "") index.push(last_index);
else {
index.push(detailed_datas[0][i]);
last_index = detailed_datas[0][i];
}
}
// Separate data from index
detailed_datas.splice(0, 1);
// Format data
var formated_datas = [];
for(var i = 0; i < detailed_datas.length; i++){
var row = {};
for(var j = 0; j < detailed_datas[i].length; j++){
// Check if value is empty
if(detailed_datas[i][j] != ""){
if(typeof row[index[j]] == "object"){
// it's already set as an array
row[index[j]].push(detailed_datas[i][j]);
} else if(row[index[j]] != undefined){
// Already have a value, so it might be an array
row[index[j]] = [row[index[j]], detailed_datas[i][j]];
} else {
// It's empty for now, so let's say first that it's a string
row[index[j]] = detailed_datas[i][j];
}
}
}
formated_datas.push(row);
}
console.log(formated_datas); // @TODO : remove this
return formated_datas;
}
I transpile and resume Wetteren's code: 我转换并恢复Wetteren的代码:
convertTSVtoJSON(tsvData) {
const formattedData = tsvData.split(/\r\n|\r|\n/g).filter(e => !!e).map((parsedEntry) => parsedEntry.split("\t"));
const tsvHeaders = formattedData.shift();
return formattedData.map(formattedEntry => {
{
return tsvHeaders.reduce((jsonObject, heading, position) => {
jsonObject[heading] = formattedEntry[position];
return jsonObject;
}, {});
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.