简体   繁体   English

无法使用d3.js在html中加载json文件

[英]Unable to load json file in html using d3.js

I want to load data from a json file in an HTML file and use D3 to visualize it. 我想从HTML文件中的json文件中加载数据,并使用D3对其进行可视化。 I am able to view the data in the browser if the file is loaded on its own. 如果文件是单独加载的,我可以在浏览器中查看数据。 The file is also received correctly verified by the 200 status. 还可以正确接收200状态验证的文件。 But the contents of the file are interpreted as null. 但是文件的内容被解释为空。

Here are the contents of the json file - 这是json文件的内容-

[{"totdistance":[[1987,20926761],[1988,81974990],[1989,93470285],[1990,116938068],[1991,128179520],[1992,123889922],[1993,113169123],[1994,119145135],[1995,132070386],[1996,145689003],[1997,158173093],[1998,164900616],[1999,180094162],[2000,191005263],[2001,186583998],[2002,185726579],[2003,189239126],[2004,203622782],[2005,200373825]],"routes":[[1987,45399],[1988,180871],[1989,195588],[1990,216314],[1991,219896],[1992,199066],[1993,176756],[1994,177851],[1995,194508],[1996,203001],[1997,206304],[1998,203071],[1999,209496],[2000,219160],[2001,209979],[2002,196111],[2003,189519],[2004,195939],[2005,197853]],"code":"HP","name":"HP","avgdelay":[[1987,16],[1988,8],[1989,11],[1990,12],[1991,12],[1992,7],[1993,11],[1994,15],[1995,17],[1996,23],[1997,17],[1998,29],[1999,25],[2000,28],[2001,18],[2002,9],[2003,13],[2004,25],[2005,18]]}] [{ “totdistance”:[[1987,20926761],[1988,81974990],[1989,93470285],[1990,116938068],[1991,128179520],[1992,123889922],[1993,113169123],[ 1994,119145135],[1995,132070386],[1996,145689003],[1997,158173093],[1998,164900616],[1999,180094162],[2000,191005263],[2001,186583998],[2002年, 185726579],[2003,189239126],[2004,203622782],[2005,200373825]], “路由”:[[1987,45399],[1988,180871],[1989,195588],[1990,216314] [1991,219896],[1992,199066],[1993,176756],[1994,177851],[1995,194508],[1996,203001],[1997,206304],[1998,203071],[ 1999,209496],[2000,219160],[2001,209979],[2002,196111],[2003,189519],[2004,195939],[2005,197853]], “代码”: “HP”, “名”: “HP”, “avgdelay”:[1987,16],[1988,8],[1989,11],[1990,12],[1991,12],[1992,7],[ 1993,11],[1994,15],[1995,17],[1996,23],[1997,17],[1998,29],[1999,25],[2000,28],[2001年, 18],[2002,9],[2003,13],[2004,25],[2005,18]]}]

Here is the code that tries to load the file and read its contents - 这是尝试加载文件并读取其内容的代码-

d3.json("flights.json", function(flights) { d3.json(“ flights.json”,function(flights){

But I get error on the console that flights is null. 但是我在控制台上收到错误消息,即flight为null。

I have tried running this code on a webserver using IE, Firefox and Chrome. 我尝试使用IE,Firefox和Chrome在网络服务器上运行此代码。 But no solution. 但是没有解决办法。

I am using this as a starting point - https://github.com/mbostock/bost.ocks.org/blob/gh-pages/mike/nations/index.html 我以此为起点-https: //github.com/mbostock/bost.ocks.org/blob/gh-pages/mike/nations/index.html

Here is the error - Uncaught TypeError: Cannot call method 'map' of null 这是错误-未捕获的TypeError:无法调用null的方法“ map”

The json is valid as per http://jsonlint.com/ json根据http://jsonlint.com/有效

Thanks for any help to fix this issue. 感谢您提供任何解决此问题的帮助。

according to this , the correct syntax of d3.json is 根据 ,正确的语法d3.json

d3.json('URL', function(error, json) {

so it appears you might be trying to access the error argument, which if there is no error, could be null. 因此看来您可能正在尝试访问错误参数,如果没有错误,则该参数可能为null。

I have switched to version 2.8 and it is working fine. 我已切换到2.8版,并且工作正常。 One thing I noticed was that I was calling one variable as avgdelay and that was an issue. 我注意到的一件事是,我将一个变量称为avgdelay,这是一个问题。 Once I renamed the variable to another one, d3 had no problems. 一旦将变量重命名为另一个变量,d3就没有问题。 I think it is kind of weird. 我认为这很奇怪。 But as of now, it has fixed the problem I was facing. 但是到目前为止,它已经解决了我面临的问题。 I am closing the question. 我要结束这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM