使用d3加载本地JSON文件

时间:2017-06-13 16:32:39

标签: javascript json d3.js

我知道JSON无法在Chrome本地加载,正如许多其他问题所讨论的那样。我正在尝试重新创建:https://romsson.github.io/dragit/example/nations.html

我试图实现硬编码json的解决方案。

工作代码:

d3.json("https://rawgit.com/romsson/dragit/master/data/nations.json", function(nations) {

...

我尝试在本地加载nations.json

var nations = [{"name":"Angola","region":"Sub-Saharan Africa","income":[[2006,12127071],[2007,12420476],[2008,12707546]],
"lifeExpectancy":[[2006,46.02],[2007,46.54],[2008,47.06]]},
{"name":"Benin","region":"Sub-Saharan Africa","income":[[2006,7862944],[2007,8078314],[2008,8294941]],
"lifeExpectancy":[[2006,60.6],[2007,61.03],[2008,61.47]]}]

d3.json(nations, function(nations) {

....

我收到错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

1 个答案:

答案 0 :(得分:1)

d3.json用于从url加载数据,而不是页面中存在的对象。因此,如果页面中已有对象,则将其传递给所需的函数。

示例

  Set rs = server.createobject("ADODB.Recordset")

  rs.Fields.Append "eventnum", 3

  rs.Open
  rs.AddNew "eventnum", "26856"
  rs.AddNew "eventnum", "26857"
  rs.AddNew "eventnum", "26857"
  rs.Update


  rs.movefirst
  while not rs.eof

    holdereventnum = rs("eventnum")
    response.write rs("eventnum") & "<br>"

    rs.movenext

    if not rs.eof then
       while holdereventnum=rs("eventnum") and not rs.eof
           rs.movenext
       wend
    end if

   wend