首页 > 解决方案 > JSON - 期待'EOF',得到','

问题描述

在 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"
            }]
    }]
}

标签: jsond3.js

解决方案


}因为您的 JSON 有多个根元素(除了末尾的额外元素)。这就是它的样子

{
  ...
}, // <-- It expects EOF but gets ','
{
  ...
}

一个有效的 JSON 应该有一个根元素,即

{
  ...
}

所以,当你添加[]它时,它变成了一个数组,现在只有一个有效的根元素,即

[
  {
    ...
  },
  {
    ...
  }
]

推荐阅读