嗨,我不明白为什么如果我在变量中写json是对的:
var theUI = {
"nodes":{"progetto 1":{"color":"red", "shape":"dot", "alpha":1},
"demos":{"color":"#b2b19d", shape:"dot", "alpha":1},
"halfviz":{"color":"#a7af00", "alpha":0, "link":""},
"atlas":{"color":"#a7af00", "alpha":0, "link":""},
"echolalia":{"color":"#a7af00", "alpha":0, "link":""},
"docs":{"color":"#b2b19d", "shape":"dot", "alpha":1},
"reference":{"color":"#922E00", "alpha":0, "link":""},
"introduction":{"color":"#922E00", "alpha":0, "link":""},
"code":{"color":"#b2b19d", "shape":"dot", "alpha":1},
"github":{"color":"orange", "alpha":0, "link":""},
".zip":{"color":"orange", "alpha":0, "link":""},
".tar.gz":{"color":"orange", "alpha":0, "link":""}
},
"edges":{
"progetto 1":{
"demos":{"length":.8},
"docs":{"length":.8},
"code":{"length":.8}
},
"demos":{"halfviz":{},
"atlas":{},
"echolalia":{}
},
"docs":{"reference":{},
"introduction":{}
},
"code":{".zip":{},
".tar.gz":{},
"github":{}
}
}
}
var sys = arbor.ParticleSystem()
sys.parameters({stiffness:900, repulsion:2000, gravity:true, dt:0.015})
sys.renderer = Renderer("#sitemap")
sys.graft(theUI)
var nav = Nav("#nav")
$(sys.renderer).bind('navigate', nav.navigate)
$(nav).bind('mode', sys.renderer.switchMode)
nav.init()
})
如果我从文件json中提供数据,则该程序不起作用:
var theUI = $.getJSON("data.json")
var sys = arbor.ParticleSystem()
sys.parameters({stiffness:900, repulsion:2000, gravity:true, dt:0.015})
sys.renderer = Renderer("#sitemap")
sys.graft(theUI)
var nav = Nav("#nav")
$(sys.renderer).bind('navigate', nav.navigate)
$(nav).bind('mode', sys.renderer.switchMode)
nav.init()
})
它执行get并读取json但程序不起作用。
文件data.json是这样的:
{
"nodes":{"progetto 1":{"color":"red", "shape":"dot", "alpha":1},
"demos":{"color":"#b2b19d", shape:"dot", "alpha":1},
"halfviz":{"color":"#a7af00", "alpha":0, "link":""},
"atlas":{"color":"#a7af00", "alpha":0, "link":""},
"echolalia":{"color":"#a7af00", "alpha":0, "link":""},
"docs":{"color":"#b2b19d", "shape":"dot", "alpha":1},
"reference":{"color":"#922E00", "alpha":0, "link":""},
"introduction":{"color":"#922E00", "alpha":0, "link":""},
"code":{"color":"#b2b19d", "shape":"dot", "alpha":1},
"github":{"color":"orange", "alpha":0, "link":""},
".zip":{"color":"orange", "alpha":0, "link":""},
".tar.gz":{"color":"orange", "alpha":0, "link":""}
},
"edges":{
"progetto 1":{
"demos":{"length":.8},
"docs":{"length":.8},
"code":{"length":.8}
},
"demos":{"halfviz":{},
"atlas":{},
"echolalia":{}
},
"docs":{"reference":{},
"introduction":{}
},
"code":{".zip":{},
".tar.gz":{},
"github":{}
}
}
}
答案 0 :(得分:3)
因为JSON中存在语法错误:
SyntaxError: test.json: Unexpected token s
第二行:
"demos":{"color":"#b2b19d", shape:"dot", "alpha":1},
shape
应该引用。
请记住,在JSON中,所有键和字符串值必须用双引号引用才能拥有有效的JSON
编辑:我现在觉得自己像个编译器。 :d因为你有下一个语法错误:
SyntaxError: test.json: Unexpected token .
这两次是以下几行:
"demos":{"length":.8},
"docs":{"length":.8},
"code":{"length":.8}
.8
不是有效值JSON。使用0.8
现在应该是它,你很高兴。
答案 1 :(得分:1)
您需要使用这样的回调函数 -
$.getJSON("data.json", function (theUI) {
var sys = arbor.ParticleSystem();
sys.parameters({
stiffness: 900,
repulsion: 2000,
gravity: true,
dt: 0.015
});
sys.renderer = Renderer("#sitemap");
sys.graft(theUI);
var nav = Nav("#nav");
$(sys.renderer).bind('navigate', nav.navigate);
$(nav).bind('mode', sys.renderer.switchMode);
nav.init();
});
此外,
jsonlint测试:http://jsonlint.com/
Parse error on line 9:
...2b19d", shape: "dot",
----------------------^
Expecting 'STRING'