Javascript 以奇怪的格式将CSV转换为JSON
我有很多geo,我想在webgl globe中使用。 Webgl的格式为 Googles.json文件在其工作的webgl globe源代码中的示例[[“1993”、[long,lat,weight,long,lat,weight],“1994”、[long,lat,weight,long,lat,weight,long,lat,weight] 我一直在寻找一种方法来转换这个,但我找不到一个转换器在线。 有没有人知道我在哪里可以找到这种格式的转换器,或者建议一种方法来实现这一点 我的数据样本:Javascript 以奇怪的格式将CSV转换为JSON,javascript,json,csv,webgl,Javascript,Json,Csv,Webgl,我有很多geo,我想在webgl globe中使用。 Webgl的格式为 Googles.json文件在其工作的webgl globe源代码中的示例[[“1993”、[long,lat,weight,long,lat,weight],“1994”、[long,lat,weight,long,lat,weight,long,lat,weight] 我一直在寻找一种方法来转换这个,但我找不到一个转换器在线。 有没有人知道我在哪里可以找到这种格式的转换器,或者建议一种方法来实现这一点 我的数据样本:
- Year Latitude Longitude Magnitude
- 1995 -82.8627519 -135 0.11
- 1995 -54.423199 3.413194 0.01
- 1994 -53.08181 73.504158 0.01
- 1994 -51.796253 -59.523613 0.04
- 1993 -49.280366 69.348557 0.02
- 1993 -41.4370868 147.1393767 0.18
进一步来看,我认为Google使用的json文件是一个嵌套的json数组。这有多种方法来解析数据。 第一步是将数据保存到文件中。 例如:
Year Latitude Longitude Magnitude
1995 -82.8627519 -135 0.11
1995 -54.423199 3.413194 0.01
1994 -53.08181 73.504158 0.01
1994 -51.796253 -59.523613 0.04
1993 -49.280366 69.348557 0.02
1993 -41.4370868 147.1393767 0.18
在raw.txt中
第二步是加载和解析数据。
在进行解析时,需要记住以下几点:
/\s{2,}/g
xhr = new XMLHttpRequest();
xhr.open('GET', '/globe/raw.txt', true);
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var lines = xhr.responseText.split("\n");//split .txt file into lines
var data = [];//prepare an array to hold the end result
var dict = {};//use an Object/Dictionary to collapse data from same key/year
for(var i = 1 ; i < lines.length; i++){//for each line
var line = lines[i].replace(/\s{2,}/g, ' ').split(' ');//collapse white spaces and split into an array of values
if( !dict[line[0]]) dict[line[0]] = [];//if there isn't an array to store that data yes, make one
dict[line[0]].push(parseFloat(line[1]));//append data into the coresponding key/year
dict[line[0]].push(parseFloat(line[2]));
dict[line[0]].push(parseFloat(line[3]));
}
for(var key in dict) data.push([key,dict[key]]);//at the end, loop through the object and populate an array
console.log(data);
}
}
};
xhr.send(null);
xhr=newXMLHttpRequest();
xhr.open('GET','/globe/raw.txt',true);
xhr.onreadystatechange=函数(e){
if(xhr.readyState==4){
如果(xhr.status==200){
var lines=xhr.responseText.split(“\n”);//将.txt文件拆分为行
var data=[];//准备一个数组来保存最终结果
var dict={};//使用对象/字典折叠来自同一关键字/年份的数据
对于(var i=1;i
因此,如果您使用类似的方法:
xhr = new XMLHttpRequest();
xhr.open('GET', '/globe/raw.txt', true);
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var lines = xhr.responseText.split("\n");//split .txt file into lines
var data = [];//prepare an array to hold the end result
var dict = {};//use an Object/Dictionary to collapse data from same key/year
for(var i = 1 ; i < lines.length; i++){//for each line
var line = lines[i].replace(/\s{2,}/g, ' ').split(' ');//collapse white spaces and split into an array of values
if( !dict[line[0]]) dict[line[0]] = [];//if there isn't an array to store that data yes, make one
dict[line[0]].push(parseFloat(line[1]));//append data into the coresponding key/year
dict[line[0]].push(parseFloat(line[2]));
dict[line[0]].push(parseFloat(line[3]));
}
for(var key in dict) data.push([key,dict[key]]);//at the end, loop through the object and populate an array
window.data = data;
for (i=0;i<data.length;i++) {
globe.addData(data[i][1], {format: 'magnitude', name: data[i][0], animated: true});
}
globe.createPoints();
settime(globe,0)();
globe.animate();
}
}
};
xhr.send(null);
xhr=newXMLHttpRequest();
xhr.open('GET','/globe/raw.txt',true);
xhr.onreadystatechange=函数(e){
if(xhr.readyState==4){
如果(xhr.status==200){
var lines=xhr.responseText.split(“\n”);//将.txt文件拆分为行
var data=[];//准备一个数组来保存最终结果
var dict={};//使用对象/字典折叠来自同一关键字/年份的数据
对于(var i=1;i 对于(i=0;i你知道,这不是JSON吗?嗨,Sirko,我下载的JSON工作文件可能会有所不同。它可能是.JSON,但不是JSON吗?你能把整个文件发布到某个地方吗?这样更容易检查格式