使用d3.json()访问嵌套JSON [英] Access nested JSON with d3.json()
本文介绍了使用d3.json()访问嵌套JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON
{
"users":
{
"gender":
{
"tot":
[
{"label":"female", "value":6038},
{"label":"male", "value":45228},
{"label":"unknown", "value":32932}
]
}
}
}
我可以加载 d3.json c $ c>并通过
console.log()
可视化,但我无法访问内部值和标签
I can load the file with d3.json()
and visualise it via console.log()
but I can't access the inner values and labels
请考虑以下脚本
d3.json("../data/m5s/sumStats.json", function(data) {
console.log(data)
x.domain(data.map(function(d) {
return d[0].users[0].gender[0].comment[0].label;
}));
});
返回第三行
Uncaught TypeError: undefined is not a function
推荐答案
您可以使用纯对象符号访问数据:
you can access the data in plain object notation:
d3.json("../data/m5s/sumStats.json", function(data) {
// these are just examples, you don't need them
console.log(data);
console.log(data.users);
console.log(data.users.gender);
console.log(data.users.gender.tot);
// this is the important bit: we're not going over 'data' but over a sub-object of data:
x.domain(data.users.gender.tot.map(function(d) { return d.label; }));
});
这篇关于使用d3.json()访问嵌套JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文