【问题标题】:Convertion from CSV to JSON in d3.js在 d3.js 中从 CSV 转换为 JSON
【发布时间】:2015-06-01 14:03:31
【问题描述】:

我在将 CSV 转换为 JSON 时遇到了困难。下面是我要转换的代码

d3.csv("http://localhost:8080/Sample/flight.csv", function(flights) {           
                //alert(flights);
              var linksByOrigin = {},
                  countByAirport = {},
                  locationByAirport = {},
                  positions = [];

              var arc = d3.geo.greatArc()
              .source(function(d) { return locationByAirport[d.source]; })
              .target(function(d) { return locationByAirport[d.target]; }); 

              //reading from csv
                flights.forEach(function(flight) {
                var origin = flight.origin,
                    destination = flight.destination,
                    links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
                links.push({source: origin, target: destination});
                countByAirport[origin] = (countByAirport[origin] || 0) + 1;
                countByAirport[destination] = (countByAirport[destination] || 0) + 1;
              });   

});

d3.json("http://localhost:8080/Sample/flight.json", function(flights) {             
                //alert(flights);
              var linksByOrigin = {},
                  countByAirport = {},
                  locationByAirport = {},
                  positions = [];

              var arc = d3.geo.greatArc()
              .source(function(d) { return locationByAirport[d.source]; })
              .target(function(d) { return locationByAirport[d.target]; }); 

              var flights = flights.flights;
              alert("flights"+flights.length);
           for(var i = 0; i < flights.length; i++)    {    
               alert("origin"+flights[i].origin+"dest"+flights[i].destination);
                 var origin = flights[i].origin;
            var  destination = flights[i].destination;
          alert("origin"+origin+"dest"+destination)
                var links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
                links.push({source: origin , target: destination});
                countByAirport[origin] = (countByAirport[origin] || 0) + 1;
                countByAirport[destination] = (countByAirport[destination] || 0) + 1;   
           }
});

当我使用 json 代码时,它给了我19:15:41.950 TypeError: _ is undefined1 d3.v2.js:1982:10 。 请告诉我我在转换时做错了什么。

【问题讨论】:

    标签: javascript json csv d3.js


    【解决方案1】:

    我需要查看您的 json 对象以获取更多信息,但基于以下假设,您的 csv 正在返回一个对象数组:

    [{origin: 'origin string', destination: 'destination string'} ... ]
    

    如果你的json存储如下:

    {flights: [{origin: 'origin string', destination: 'destination string'}, ...]}
    

    那么你的代码应该可以工作了。如果您的 json 格式不同,如果您可以上传示例 csv 和 json 文件,我可以给您一个更好的答案。

    很难说这个错误有什么问题,但我猜你的 json 文件中没有设置航班变量。

    【讨论】:

    • 这里是我的航班 json - {"flights":[{"origin":"6972475","destination":"1411474","count":"1853"}, {"origin":"6972475","destination":"4683455","count":"2853"} ]} 和我的航班 csv origin,destination,count 6972475,1411474,2853 6972475,4683455,1853
    • 它现在正在工作。在构建 json 时需要非常小心。我所做的错误是我留下了一个空格。而不是"destination":"1411474",它就像"destination":"1411474 "。这就是为什么抛出错误。
    • 太好了,你设法弄明白了!如果没有完整的 json 和代码,将无法在这里找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2021-01-08
    • 2021-03-11
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    相关资源
    最近更新 更多