我在网上搜索过,而大多数人都是直接从 url 或本地文件夹读取 .json 文件 喜欢
console.log("before csv ");
d3.csv("cities.csv", function(data) {console.log(data)});
console.log("before json");
d3.json("flare.json",function(error,data2) {console.log(error, data2)});
目前我的要求是我有一个 json 格式的字符串,例如
{"nodes": [
{"id": "Myriel"},
{"id": "Napoleon"},
{"id": "Mlle.Baptistine"},
{"id": "Mme.Magloire"},
{"id": "CountessdeLo"},
{"id": "Geborand"},
{"id": "Champtercier"},
{"id": "Cravatte"},
{"id": "Count"},
{"id": "OldMan"},
{"id": "Labarre"},
{"id": "Valjean"},
{"id": "Marguerite"},
{"id": "Mme.deR"}
]}
有没有一种方法可以直接处理这个字符串作为从 .json 或 .csv 文件读取的数据?
最佳答案
D3 仅适用于 JS 对象,d3.json 只是从外部文件加载对象的方法。因此,如果您不需要从外部文件加载数据,则不要使用 d3.json。
您可以这样做的方法之一:
var jsonObj = JSON.parse(data);
关于javascript - 我可以使用 d3.json 处理类似于 json 格式的字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075086/