以下JSON有什么问题?

时间:2014-09-19 21:44:07

标签: json

{
    "id": 1001,
    "text": "Core Java - Collections",
    "description": "public static void main(String args[ ]) { \n int x =10;\n}",
    "passingScore": 10,
    "totalQuestions": 20,
    "technologyId": 10,
    "subTechnologyId": 101
}{ // fails on this line
    "id": 1000,
    "text": "Core Java",
    "description": "public static void main(String args[ ]) { \n int x =10;\n}",
    "passingScore": 10,
    "totalQuestions": 5,
    "technologyId": 10,
    "subTechnologyId": 100
}

我没有看到上述JSON的任何错误,但不知怎的,这失败了。有人可以帮我发现错误吗?

2 个答案:

答案 0 :(得分:3)

主要问题是您只能拥有一个顶级项目,该项目必须是对象({})或数组([])。第二个问题是,如果您有多个项目,则必须用逗号分隔它们。

例如,这是有效的:

[
    {
        "id": 1001,
        "text": "Core Java - Collections",
        "description": "public static void main(String args[ ]) { \n int x =10;\n}",
        "passingScore": 10,
        "totalQuestions": 20,
        "technologyId": 10,
        "subTechnologyId": 101
    },
    {
        "id": 1000,
        "text": "Core Java",
        "description": "public static void main(String args[ ]) { \n int x =10;\n}",
        "passingScore": 10,
        "totalQuestions": 5,
        "technologyId": 10,
        "subTechnologyId": 100
    }
]

我已添加[]以使其成为数组,并在对象之间添加逗号。

或者,如果它不应该是一个数组,你可以使它成为一个有两个键的对象:

{
    "first": {
        "id": 1001,
        "text": "Core Java - Collections",
        "description": "public static void main(String args[ ]) { \n int x =10;\n}",
        "passingScore": 10,
        "totalQuestions": 20,
        "technologyId": 10,
        "subTechnologyId": 101
    },
    "second": {
        "id": 1000,
        "text": "Core Java",
        "description": "public static void main(String args[ ]) { \n int x =10;\n}",
        "passingScore": 10,
        "totalQuestions": 5,
        "technologyId": 10,
        "subTechnologyId": 100
    }
}

答案 1 :(得分:0)

这不是JSON。 JSON文档是数组或字典。你有两本词典。喜欢 { } { }。您需要一个以逗号分隔的字典数组。喜欢 [ { }, { } ]。

解决这个问题:告诉谁提供所谓的“JSON”来修复它。另外,您可以手动解析任何内容。实际上,您所需要的只是找到匹配的结束括号,将数据拆分为多个并分别解析它们。十分难看;我拒绝这样做。