gpt4 book ai didi

jquery - 为什么程序无法读取文件 json?

转载 作者:行者123 更新时间:2023-12-01 07:18:24 25 4
gpt4 key购买 nike

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

最佳答案

因为 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

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

关于jquery - 为什么程序无法读取文件 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16628527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com