需要JSON模式数组

时间:2015-03-10 12:12:49

标签: arrays json jsonschema

我正在试图弄清楚如何设置架构中所需的全局级数组。我的示例JSON文件是:

[
    {
        "firstname": "Paul",
        "lastname": "McCartney"
    },
    {
        "firstname": "John",
        "lastname": "Lennon"
    },
    {
        "firstname": "George",
        "lastname": "Harrison"
    },
    {
        "firstname": "Ringo",
        "lastname": "Starr"
    }
]

如上所示,我希望顶级结构是一个数组,而不是一个对象。我从jsonschema.net获得的模式是(稍作修改):

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "/",
  "type": "array",
  "items": {
    "id": "elements",
    "type": "object",
    "properties": {
      "firstname": {
        "id": "firstname",
        "type": "string"
      },
      "lastname": {
        "id": "lastname",
        "type": "string"
      }
    },
    "required": [
      "firstname",
      "lastname"
    ]
  },
  "required": [
    "/"
  ]
}

但它失败了jsonschema validator。能否帮助我为顶级阵列提供正确的JSON模式?

1 个答案:

答案 0 :(得分:1)

要使用输入数据有效,您只需要遵循架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "array",
    "id": "http://jsonschema.net",
    "items": {
        "type": "object",
        "properties": {
        "firstname": {
            "type": "string"
        },
        "lastname": {
            "type": "string"
        }
    },
    "required": [
        "firstname",
        "lastname"
    ]}
}