无效的JSON-试图嵌套数组
Invalid JSON - trying to nest an array
我正在尝试构建一个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/
相关文章:
- 从多维嵌套json数组创建下拉列表
- 如何通过json对象数组为嵌套对象赋值
- 为循环嵌套的Javascript未按预期返回数组
- 具有嵌套对象数组的 Javascript 对象的递归搜索函数
- 将js对象更改为使用嵌套的可观察数组敲除js视图模型
- MongoDB嵌套对象数组后查询
- 从对象数组中动态创建嵌套json
- 使用Angular.js解析JSON中的嵌套对象数组
- 如何将id数组与带下划线的对象数组嵌套属性进行比较
- 将JS对象数组转换为嵌套形式的最有效方法
- 在javascript/angular中创建播放列表(按值复制数组,但按引用设置嵌套对象)
- 嵌套在 ng 重复中的拼接数组
- 嵌套/同心组和mouseenter/mouseleve
- javascript获取嵌套子数组中的对象
- 嵌套捕获组结果
- 如何在嵌套级别不受限制的情况下显示对象的动态嵌套子数组对象
- 按嵌套子数组对 arr 进行排序
- 播放框架 - 表单不绑定到嵌套元组
- 遍历嵌套到组和节中的一组输入
- 在数组和嵌套子数组中搜索Lodash属性