Javascript 无效的JSON-尝试嵌套数组
我正在尝试构建一个JSON对象来驱动我的web应用程序,但是我当前返回了一个无效的JSON对象,我看不到问题所在 我已经运行了JSON,我得到了以下错误 第19行的分析错误: ... ] },Javascript 无效的JSON-尝试嵌套数组,javascript,json,Javascript,Json,我正在尝试构建一个JSON对象来驱动我的web应用程序,但是我当前返回了一个无效的JSON对象,我看不到问题所在 我已经运行了JSON,我得到了以下错误 第19行的分析错误: ... ] }, ----------------------^ 应为“STRING”、“NUMBER”、“NULL”、“TRUE”、“FALSE”、“{”、“[” 我的JSON对象在下面 { "name": "FF", "good": {
----------------------^ 应为“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应该如何形成?数组项后有一个额外的逗号,就在这里:
"specifics" : [
"Specifics 1b",
"Specifics 2b",
"Specifics 3b", //there is no next element, remove the ,
]
同样的情况也发生在这里:
"specifics" : [
"Chair Rail A",
]
walls属性后还有一个额外的逗号:
"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将被解析。在这些情况下,我总是发现这是一个有用的工具。这里的问题是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中使用了太多逗号
我用它来验证JSON,我发现它对于突出显示错误非常有用。您的JSON格式无效,代码中有大量的
,
,请在线验证JSON代码
在线验证工具:
“Specifics 3b”,正如@MichaelKunst指出的那样,
。额外的逗号..删除它注意,虽然大多数JavaScript实现在对象或数组的末尾接受一个尾随逗号,但JSON比JavaScript更严格,尾随逗号被明确禁止。例如,[“a”,]
是有效的JavaScript,但是JSON.stringify(“[”a“,]”
)将失败(对于对象也是如此)。