使用 d3.json 导入本地 json 文件不起作用 [英] Importing local json file using d3.json does not work

查看:63
本文介绍了使用 d3.json 导入本地 json 文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 d3.json() 导入本地 .json 文件.

I try to import a local .json-file using d3.json().

文件 filename.json 与我的 html 文件存储在同一文件夹中.

The file filename.json is stored in the same folder as my html file.

然而 (json) 参数为空.

Yet the (json)-parameter is null.

d3.json("filename.json", function(json) {
    root = json;
    root.x0 = h / 2;
    root.y0 = 0;});
    . . . 
}

我的代码与这个d3.js示例

推荐答案

如果您在浏览器中运行,您无法加载本地文件.

If you're running in a browser, you cannot load local files.

但是在命令行上运行开发服务器相当容易,只需将 cd 放入包含文件的目录中,然后:

But it's fairly easy to run a dev server, on the commandline, simply cd into the directory with your files, then:

python -m SimpleHTTPServer

(或 python -m http.server 使用 python 3)

(or python -m http.server using python 3)

现在在您的浏览器中,转到 localhost:3000(或 :8000 或命令行上显示的任何内容).

Now in your browser, go to localhost:3000 (or :8000 or whatever is shown on the commandline).

以下用于旧版本的 d3:

The following used to work in older versions of d3:

var json = {"my": "json"};
d3.json(json, function(json) {
    root = json;
    root.x0 = h / 2;
    root.y0 = 0;
});

这篇关于使用 d3.json 导入本地 json 文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆