在jsonlint上,下面的JSON显示了一个错误:期望'EOF‘,got ',’‘。问题显示在第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显示为有效。
但是,我使用的是d3.js中的JSON,它不允许外部方括号。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"
}]
}]
}发布于 2019-05-14 08:48:13
您有多个根对象,因此不是有效的JSON。根可以是对象{}或数组[]。
在你的例子中,我会选择数组:
[
{
"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"
}
]
}
}发布于 2019-05-14 08:49:27
因为JSON有多个根元素(除了末尾有一个额外的} )。这就是它看起来的样子
{
...
}, // <-- It expects EOF but gets ','
{
...
}一个有效的JSON应该有一个根元素,即,
{
...
}因此,当您添加[]时,它变成了一个数组,现在具有一个有效的根元素,即,
[
{
...
},
{
...
}
]https://stackoverflow.com/questions/56126313
复制相似问题