【问题标题】:JSON - Expecting 'EOF', got ','JSON - 期待'EOF',得到','
【发布时间】:2019-05-15 09:26:36
【问题描述】:

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

【问题讨论】:

    标签: json d3.js


    【解决方案1】:

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

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

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

    {
      ...
    }
    

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

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

    【讨论】:

      【解决方案2】:

      您有多个根对象,因此不是有效的 JSON。根可以是 Object {} 或 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"
                  }
              ]
          }
      }
      

      【讨论】:

      • 我知道这是有效的,但 d3.js 不允许外方括号。
      • @IlludiumPu36 我添加了一个对象版本,你需要指定像“a”、“b”或任何你想要的键
      猜你喜欢
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      相关资源
      最近更新 更多