Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以奇怪的格式将CSV转换为JSON_Javascript_Json_Csv_Webgl - Fatal编程技术网

Javascript 以奇怪的格式将CSV转换为JSON

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] 我一直在寻找一种方法来转换这个,但我找不到一个转换器在线。 有没有人知道我在哪里可以找到这种格式的转换器,或者建议一种方法来实现这一点 我的数据样本:

我有很多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中

第二步是加载和解析数据。 在进行解析时,需要记住以下几点:

  • 原始数据中分隔值的空格数量不一致,因此需要先折叠这些空格,以便我们可以按空格字符分割行/行。幸运的是,数据中不包含包含空格的名称,因此我们可以使用类似so
    /\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吗?你能把整个文件发布到某个地方吗?这样更容易检查格式