json - 使用 d3.json 导入本地 json 文件不起作用

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

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

然而 (json) 参数为空。

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

我的代码和这个d3.js example里的基本一样

最佳答案

如果您在浏览器中运行,则 cannot load local files .

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

python -m SimpleHTTPServer

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

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


以下内容曾经在旧版本的 d3 中工作:

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

https://stackoverflow.com/questions/17214293/

相关文章:

json - 对于 Restful API,GET 方法可以使用 json 数据吗?

json - Jackson json反序列化,忽略json中的根元素

java - 将 JSONObject 转换为 Map

javascript - JSON 对象的增量编码

json - 在 JSON 中禁用超文本应用程序语言 (HAL)?

javascript - 如何确定性地验证 JSON 对象是否未被修改?

javascript - 是否可以通过适当的 JavaScript 字符串转义来利用 JSON 响应

json - 如何解析嵌套 JSON 对象中的内部字段

python - 用 json 编码元组的最佳方法

java - @JsonUnwrapped 的 Jackson 反序列化等价物是什么?