1

嗨,我不明白为什么如果我在变量中写入 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()
})

它执行获取并读取 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":{}
}
}
}
4

2 回答 2

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

现在应该是这样了,你可以开始了。

于 2013-05-18T20:15:09.323 回答
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'
于 2013-05-18T20:15:20.920 回答