如何使用d3.js导航JSON嵌套文件?

时间:2014-05-21 20:03:46

标签: javascript json d3.js

提前感谢您的帮助。这是我的问题,我找不到探索json嵌套文件并尝试。这是我的json文件

 {"Id":466,"Name":"korea",
"Occurrences":                                                                                                                                                                                                                                                            
 [
   {"OccurrenceDate":"\/Date(1398207600000+0100)\/","OccurrenceFrequency":27},
   {"OccurrenceDate":"\/Date(1398726000000+0100)\/","OccurrenceFrequency":1},
   {"OccurrenceDate":"\/Date(1398898800000+0100)\/","OccurrenceFrequency":4},
   {"OccurrenceDate":"\/Date(1399071600000+0100)\/","OccurrenceFrequency":303}]}

这是我的代码Javascript代码:

  <script src="http://d3js.org/d3.v3.min.js"></script>
 <script>



 d3.json("data2.json", function(error, data) {
  data.forEach(function(d) {

    console.log(d.Id)
    console.log(d.Name)
    console.log(d.Occurrence.OccurrenceFrequency)
    console.log(d.Occurrence.OccurrenceDate)})

    return d;
  });
}); 
</script>

真的在寻找帮助

1 个答案:

答案 0 :(得分:1)

试一试。鉴于您的格式是一个对象,您不能在其上使用forEach,但它似乎不是您需要的。

d3.json("data2.json", function(error, data) {
  console.log(data.Id)
  console.log(data.Name)
  data.Occurences.forEach(function(d) {

    console.log(d.OccurrenceFrequency)
    console.log(d.OccurrenceDate)})

    return d;
  });
});