验证json数组模式

时间:2016-09-06 11:20:51

标签: javascript jquery json

我有这个json的人



var menu = [{
	name: 'Computers',
	children: [{
		name: 'Notebook'
		children: [{
			name: 'Apple'
		}, {
			name: 'Windows'
		}]
	}, {
		name: Tablets
		children: [{
			name: 'Apple'
		}, {
			name: 'Android'
		}, {
			name: 'Windows'
		}]
	}]
}, {
	name: 'Phones',
	children: [{
		name: 'Android'
		children: [{
			name: 'Samsung'
		}, {
			name: 'Nokia'
		}, {
			name: 'Lenovo'
		}]
	}, {
		name: 'Windows Phones'
		children: [{
			name: 'Microsoft'
		}, {
			name: 'Nokia'
		}]
	}]
}, {
	name: 'Cameras',
	children: [{
		name: 'Digital'
		children: [{
			name: 'Nikon'
		}, {
			name: 'Fuji'
		}]
	}, {
		name: 'DSLR'
		children: [{
			name: 'Canon'
		}, {
			name: 'Nikon'
		}]
	}]
}];



 它说这是无效的json ...所以如何让它有效json? 任何帮助将不胜感激...非常感谢 顺便说一句我是初学者所以请帮助我

任何建议?再次感谢:)

4 个答案:

答案 0 :(得分:0)

你有一些丢失的逗号,注明并且在你的object字面上是一个没有分隔符的字符串。



var menu = [{
    name: 'Computers',
    children: [{
        name: 'Notebook', // missing ,
        children: [{
            name: 'Apple'
        }, {
            name: 'Windows'
        }]
    }, {
        name: 'Tablets', // missing string delimiter and comma
        children: [{
            name: 'Apple'
        }, {
            name: 'Android'
        }, {
            name: 'Windows'
        }]
    }]
}, {
    name: 'Phones',
    children: [{
        name: 'Android', // missing ,
        children: [{
            name: 'Samsung'
        }, {
            name: 'Nokia'
        }, {
            name: 'Lenovo'
        }]
    }, {
        name: 'Windows Phones', // missing ,
        children: [{
            name: 'Microsoft'
        }, {
            name: 'Nokia'
        }]
    }]
}, {
    name: 'Cameras',
    children: [{
        name: 'Digital', // missing ,
        children: [{
            name: 'Nikon'
        }, {
            name: 'Fuji'
        }]
    }, {
        name: 'DSLR', // missing ,
        children: [{
            name: 'Canon'
        }, {
            name: 'Nikon'
        }]
    }]
}];

console.log(menu);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:0)

有两个问题

  • 在名称属性值之后,comma缺失(在各个地方)
  • Tablets不在引号中

正确的语法是。

var menu = [{
    name: 'Computers',
    children: [{
        name: 'Notebook',
        children: [{
            name: 'Apple'
        }, {
            name: 'Windows'
        }]
    }, {
        name: 'Tablets',
        children: [{
            name: 'Apple'
        }, {
            name: 'Android'
        }, {
            name: 'Windows'
        }]
    }]
}, {
    name: 'Phones',
    children: [{
        name: 'Android',
        children: [{
            name: 'Samsung'
        }, {
            name: 'Nokia'
        }, {
            name: 'Lenovo'
        }]
    }, {
        name: 'Windows Phones',
        children: [{
            name: 'Microsoft'
        }, {
            name: 'Nokia'
        }]
    }]
}, {
    name: 'Cameras',
    children: [{
        name: 'Digital',
        children: [{
            name: 'Nikon'
        }, {
            name: 'Fuji'
        }]
    }, {
        name: 'DSLR',
        children: [{
            name: 'Canon'
        }, {
            name: 'Nikon'
        }]
    }]

}];

您可以复制代码是chrome的控制台,以查看错误的位置。

答案 2 :(得分:0)

你需要在键上加上双引号,例如“key”:“value”

 [{
    "name": "Computers",
    "children": [{
        "name": "Notebook",
        "children": [{
            "name": "Apple"
        }, {
            "name": "Windows"
        }]
    }, {
        "name": "Tablets",
        "children": [{
            "name": "Apple"
        }, {
            "name": "Android"
        }, {
            "name": "Windows"
        }]
    }]
}]

在名称属性值之后,缺少逗号(在不同的地方)

答案 3 :(得分:0)

键和值应该用双引号括起来

复制粘贴代码验证https://jsonformatter.curiousconcept.com/

     [  
       {  
          "name":"Computers",
          "children":[  
             {  
                "name":"Notebook",
                "children":[  
                   {  
                      "name":"Apple"
                   },
                   {  
                      "name":"Windows"
                   }
                ]
             },
             {  
                "name":"Tablets",
                "children":[  
                   {  
                      "name":"Apple"
                   },
                   {  
                      "name":"Android"
                   },
                   {  
                      "name":"Windows"
                   }
                ]
             }
          ]
       },
       {  
          "name":"Phones",
          "children":[  
             {  
                "name":"Android",
                "children":[  
                   {  
                      "name":"Samsung"
                   },
                   {  
                      "name":"Nokia"
                   },
                   {  
                      "name":"Lenovo"
                   }
                ]
             },
             {  
                "name":"Windows Phones",
                "children":[  
                   {  
                      "name":"Microsoft"
                   },
                   {  
                      "name":"Nokia"
                   }
                ]
             }
          ]
       },
       {  
          "name":"Cameras",
          "children":[  
             {  
                "name":"Digital",
                "children":[  
                   {  
                      "name":"Nikon"
                   },
                   {  
                      "name":"Fuji"
                   }
                ]
             },
             {  
                "name":"DSLR",
                "children":[  
                   {  
                      "name":"Canon"
                   },
                   {  
                      "name":"Nikon"
                   }
                ]
             }
          ]
       }
    ]