簡體   English   中英

將CSV格式轉換為JSON格式

[英]Converting CSV to JSON in a strange format

我有很多我想在webgl globe中使用的geo。 Webgl的格式為

Googles .json文件在其工作的webgl全球來源中的示例[["1993",[long, lat,weight,long, lat,weight],["1994",[long, lat,weight,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中

第二步是加載和解析數據。 進行解析時,需要牢記以下幾點:

  1. 原始數據的空格分隔符數量不一致,因此需要首先折疊空格,以便我們可以用空格字符分隔行/行。 幸運的是,數據不包含包含空格的名稱,因此我們可以像這樣使用RegEx /\\s{2,}/g
  2. 我們希望將與一年有關的所有數據收集到一個列表中。 一種方法是使用數組並連續檢查它是否已經具有年份值。 另一個是只使用對象/關聯數組/字典,而不用擔心任何檢查。
  3. 一旦將數據正確地收集到一個對象中,我們就會將其彈出到一個數組中,以便與所用數據的格式匹配。

這就是我的意思:

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 = 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);

在服務器上運行的WebGL Globe實驗中 ,您將看到您的數據。 WebGL地球

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM