JSON 值为对象数组的对象的架构
JSON schema for an object whose value is an array of objects
我正在编写一个可以从文件中读取 JSON 数据的软件。该文件包含 "person" - 一个对象,其值为对象数组。我打算使用 JSON 模式验证库来验证内容,而不是自己编写代码。符合代表以下数据的 JSON Schema Draf-4 的正确模式是什么?
{
"person" : [
{
"name" : "aaa",
"age" : 10
},
{
"name" : "ddd",
"age" : 11
},
{
"name" : "ccc",
"age" : 12
}
]
}
记下的架构如下。不知是否正确,是否有其他形式?
{
"person" : {
"type" : "object",
"properties" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"name" : {"type" : "string"},
"age" : {"type" : "integer"}
}
}
}
}
}
你实际上只有一行错了地方,但那一行破坏了整个架构。 "person" 是对象的 属性,因此必须在 properties
关键字下。通过将 "person" 放在顶部,JSON Schema 将其解释为关键字而不是 属性 名称。由于没有 person
关键字,JSON Schema 会忽略它和它下面的所有内容。因此,它与针对空模式 {}
进行验证相同,后者对 JSON 文档可以包含的内容没有任何限制。任何有效的 JSON 都对空模式有效。
{
"type" : "object",
"properties" : {
"person" : {
"type" : "array",
"items" {
"type" : "object",
"properties" : {
"name" : {"type" : "string"}
"age" : {"type" : "integer"}
}
}
}
}
}
顺便说一下,有几种在线 JSON 架构测试工具可以在您制作架构时为您提供帮助。这是我的 goto http://jsonschemalint.com/draft4/#
此外,这里有一个很棒的 JSON 架构参考,也可能对您有所帮助:https://spacetelescope.github.io/understanding-json-schema/
我正在编写一个可以从文件中读取 JSON 数据的软件。该文件包含 "person" - 一个对象,其值为对象数组。我打算使用 JSON 模式验证库来验证内容,而不是自己编写代码。符合代表以下数据的 JSON Schema Draf-4 的正确模式是什么?
{
"person" : [
{
"name" : "aaa",
"age" : 10
},
{
"name" : "ddd",
"age" : 11
},
{
"name" : "ccc",
"age" : 12
}
]
}
记下的架构如下。不知是否正确,是否有其他形式?
{
"person" : {
"type" : "object",
"properties" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"name" : {"type" : "string"},
"age" : {"type" : "integer"}
}
}
}
}
}
你实际上只有一行错了地方,但那一行破坏了整个架构。 "person" 是对象的 属性,因此必须在 properties
关键字下。通过将 "person" 放在顶部,JSON Schema 将其解释为关键字而不是 属性 名称。由于没有 person
关键字,JSON Schema 会忽略它和它下面的所有内容。因此,它与针对空模式 {}
进行验证相同,后者对 JSON 文档可以包含的内容没有任何限制。任何有效的 JSON 都对空模式有效。
{
"type" : "object",
"properties" : {
"person" : {
"type" : "array",
"items" {
"type" : "object",
"properties" : {
"name" : {"type" : "string"}
"age" : {"type" : "integer"}
}
}
}
}
}
顺便说一下,有几种在线 JSON 架构测试工具可以在您制作架构时为您提供帮助。这是我的 goto http://jsonschemalint.com/draft4/#
此外,这里有一个很棒的 JSON 架构参考,也可能对您有所帮助:https://spacetelescope.github.io/understanding-json-schema/