1

I want to load data from a json file in an HTML file and use D3 to visualize it. 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. But the contents of the file are interpreted as null.

Here are the contents of the json file -

[{"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]]}]

Here is the code that tries to load the file and read its contents -

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

But I get error on the console that flights is null.

I have tried running this code on a webserver using IE, Firefox and 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

Here is the error - Uncaught TypeError: Cannot call method 'map' of null

The json is valid as per http://jsonlint.com/

Thanks for any help to fix this issue.

4

2 回答 2

1

我已经切换到 2.8 版,它工作正常。我注意到的一件事是我将一个变量称为 avgdelay,这是一个问题。一旦我将变量重命名为另一个变量,d3 就没有问题了。我觉得这有点奇怪。但截至目前,它已经解决了我面临的问题。我正在结束这个问题。

于 2013-08-29T03:12:32.510 回答
1

据此正确的语法d3.json

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

所以看起来您可能正在尝试访问错误参数,如果没有错误,它可能为空。

于 2013-08-23T00:22:39.620 回答