无效的JSON - 尝试嵌套数组

时间:2013-12-02 10:22:03

标签: javascript json

我正在尝试构建一个可以驱动我的Web应用程序的JSON对象,但是我当前正在返回一个无效的JSON对象,我看不到问题,

我通过JSON Lint运行了JSON,我收到以下错误,

  

第19行的解析错误:   ......]},
  ---------------------- ^   期待'STRING','NUMBER','NULL','TRUE','FALSE','{','['

我的JSON对象在下面,

    {
    "name": "FF",
    "good": {

        "doors" : [
            {
                "name" : "Door Name 1",
                "thumb": "http://placehold.it/134x134/ff0000/ffffff",
                "specifics" : [
                     "Specifics 1a",
                    "Specifics 2a"
                ]   
            },
            {
                "name" : "Door Name 2",
                "thumb": "http://placehold.it/134x134/b4da55/ffffff",
                "specifics" : [
                    "Specifics 1b",
                    "Specifics 2b",
                    "Specifics 3b",
                ]   
            }, //LINE 19 - Where the JSON Lint states there to be an error.
            {
                "name" : "Door Name 3",
                "thumb": "http://placehold.it/134x134/0000ff/ffffff",
                "specifics" : [
                     "Specifics 1c",
                    "Specifics 2c",
                    "Specifics 3c",
                    "Specifics 4c"
                ]   
            },
        ],
        "walls" : [
            {
                "name" : "Chair Rail A",
                "thumb": "http://placehold.it/134x134/0000ff/ffffff",
                "specifics" : [
                    "Chair Rail A",
                ]   
            },
            {
                "name" : "Wall Paneling with Rosettes A",
                "thumb": "http://placehold.it/134x134/b4da55/ffffff",
                "specifics" : [
                    "Panel Moulding A",
                    "4\" Rossette"
                ]   
            },
            {
                "name" : "Wall Paneling with Rossettes B",
                "thumb": "http://placehold.it/134x134/ff0000/ffffff",
                "specifics" : [
                    "Panel Moulding A",
                    "6\" Rossette"
                ]   
            },
        ],

    },
    "best": {},
    "better": {}
}

我认为问题来自于尝试在对象中使用数组,因此在循环时我可以有多个选项,这是正确的吗?如果是这样,我的JSON应该如何形成?

4 个答案:

答案 0 :(得分:1)

在JSON的许多地方,你都有悬空逗号。摆脱那些,你的JSON将解析。在这些场合,我总觉得jsonlint.com是一个有用的工具。

答案 1 :(得分:0)

数组项后面有一个额外的逗号,就在这里:

    "specifics" : [
        "Specifics 1b",
        "Specifics 2b",
        "Specifics 3b", //there is no next element, remove the ,
    ]  

同样的情况也出现在这里:

    "specifics" : [
        "Chair Rail A",
    ]   

在wall属性之后还有一个额外的逗号:

"walls" : [
    {
        "name" : "Chair Rail A",
        "thumb": "http://placehold.it/134x134/0000ff/ffffff",
        "specifics" : [
            "Chair Rail A",
        ]   
    },
    {
        "name" : "Wall Paneling with Rosettes A",
        "thumb": "http://placehold.it/134x134/b4da55/ffffff",
        "specifics" : [
            "Panel Moulding A",
            "4\" Rossette"
        ]   
    },
    {
        "name" : "Wall Paneling with Rossettes B",
        "thumb": "http://placehold.it/134x134/ff0000/ffffff",
        "specifics" : [
            "Panel Moulding A",
            "6\" Rossette"
        ]   
    },
], //Remove me

答案 2 :(得分:0)

这里的问题是JSON格式错误。这是正确的格式:

{
"name": "FF",
"good": {

    "doors" : [
        {
            "name" : "Door Name 1",
            "thumb": "http://placehold.it/134x134/ff0000/ffffff",
            "specifics" : [
                 "Specifics 1a",
                "Specifics 2a"
            ]   
        },
        {
            "name" : "Door Name 2",
            "thumb": "http://placehold.it/134x134/b4da55/ffffff",
            "specifics" : [
                "Specifics 1b",
                "Specifics 2b",
                "Specifics 3b"
            ]   
        },
        {
            "name" : "Door Name 3",
            "thumb": "http://placehold.it/134x134/0000ff/ffffff",
            "specifics" : [
                 "Specifics 1c",
                "Specifics 2c",
                "Specifics 3c",
                "Specifics 4c"
            ]   
        }
    ],
    "walls" : [
        {
            "name" : "Chair Rail A",
            "thumb": "http://placehold.it/134x134/0000ff/ffffff",
            "specifics" : [
                "Chair Rail A"
            ]   
        },
        {
            "name" : "Wall Paneling with Rosettes A",
            "thumb": "http://placehold.it/134x134/b4da55/ffffff",
            "specifics" : [
                "Panel Moulding A",
                "4\" Rossette"
            ]   
        },
        {
            "name" : "Wall Paneling with Rossettes B",
            "thumb": "http://placehold.it/134x134/ff0000/ffffff",
            "specifics" : [
                "Panel Moulding A",
                "6\" Rossette"
            ]   
        }
    ]

},
"best": {},
"better": {}
}

你在json中使用过多逗号。

我使用jsoneditoronline.org来验证JSON,我发现它对突出显示的错误非常有用。

答案 3 :(得分:0)

你的json格式无效,你的代码中有很多,,在线验证你的json代码。

在线验证工具:http://jsonformatter.curiousconcept.com/