无效的JSON-试图嵌套数组

Invalid JSON - trying to nest an array

本文关键字:嵌套 数组 JSON- 无效      更新时间:2024-01-27

我正在尝试构建一个JSON对象来驱动我的web应用程序,但我目前返回了一个无效的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应该如何形成?

在JSON中的许多位置都有悬空逗号。去掉这些,JSON就会解析。在这种情况下,我总是觉得jsonlint.com是一个有用的工具。

这里的问题是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,发现它对突出显示错误非常有用。

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

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

同样的情况也发生在这里:

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

墙属性后面还有一个额外的逗号:

"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

如果json格式无效,代码中有很多,,请在线验证json代码。

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