值为对象数组的对象的JSON模式

qoefvg9y  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在编写一个可以从文件中读取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"}
            }
         }
      }
   }
}
lf5gs5x2

lf5gs5x21#

实际上只有一行放错了地方,但这一行破坏了整个架构。“person”是对象的属性,因此必须在properties关键字下。通过将“person”放在顶部,JSON架构将其解释为关键字而不是属性名称。由于没有person关键字,JSON架构将忽略它及其下面的所有内容。因此,这与针对空模式{}进行验证是一样的,空模式{}对JSON文档可以包含的内容没有任何限制,任何有效的JSON对空模式都是有效的。

{
   "type" : "object",
   "properties" : {
      "person" : {
         "type" : "array",
         "items": {
            "type" : "object",
            "properties" : {
               "name" : {"type" : "string"}
               "age" : {"type" : "integer"}
            }
         }
      }
   }
}

顺便说一句,有几个在线JSON模式测试工具可以帮助您创建模式。http://jsonschemalint.com/draft4/#
另外,这里有一个很好的JSON模式参考,可能也会对您有所帮助:https://spacetelescope.github.io/understanding-json-schema/

相关问题