在jsonlint上,以下JSON显示错误:预期为'EOF',得到了','。问题在第10行显示为逗号。
{
"name": "Professional",
"children": [{
"name": "Professional Behavours"
}, {
"name": "Self-Care and Self-Awareness"
}, {
"name": "Medical Ethics and Law"
}]
}, {
"name": "Leader",
"children": [{
"name": "Teamwork and Leadership"
}, {
"name": "Collaborative Practice"
}, {
"name": "Health Systems and Careers"
}]
}
}
添加外部方括号后,JSON显示为有效。
但是,我将JSON与d3.js一起使用,不允许使用外部方括号。 d3.js也无法识别下面的JSON。
应如何在没有外部方括号的情况下正确格式化此JSON?
更新
感谢答案,这项工作有效:
{
"name": "Condition",
"children": [{
"name": "Professional",
"children": [{
"name": "Professional Behavours"
},{
"name": "Self-Care and Self-Awareness"
},{
"name": "Medical Ethics and Law"
}]
}, {
"name": "Leader",
"children": [{
"name": "Teamwork and Leadership"
},{
"name": "Collaborative Practice"
},{
"name": "Health Systems and Careers"
}]
}]
}
答案 0 :(得分:3)
因为您的JSON具有多个根元素(除了末尾的额外}
外)。看起来就是这样
{
...
}, // <-- It expects EOF but gets ','
{
...
}
有效的JSON应该具有一个根元素,即
{
...
}
因此,当您添加[]
时,它将成为一个数组,并且现在具有单个有效的根元素,即
[
{
...
},
{
...
}
]
答案 1 :(得分:2)
您有多个根对象,因此不是有效的JSON。根可以是对象{}或数组[]。
在您的情况下,我要使用Array:
[
{
"name": "Professional",
"children": [
{
"name": "Professional Behavours"
},
{
"name": "Self-Care and Self-Awareness"
},
{
"name": "Medical Ethics and Law"
}
]
},
{
"name": "Leader",
"children": [
{
"name": "Teamwork and Leadership"
},
{
"name": "Collaborative Practice"
},
{
"name": "Health Systems and Careers"
}
]
}
]
或者如果您希望将根作为对象:
{
"a" : {
"name": "Professional",
"children": [
{
"name": "Professional Behavours"
},
{
"name": "Self-Care and Self-Awareness"
},
{
"name": "Medical Ethics and Law"
}
]
},
"b": {
"name": "Leader",
"children": [
{
"name": "Teamwork and Leadership"
},
{
"name": "Collaborative Practice"
},
{
"name": "Health Systems and Careers"
}
]
}
}