元组数组的JSON模式

时间:2015-12-17 12:15:41

标签: javascript json json.net jsonschema

提前致谢。

我是JSON& JSON模式。试图为元组数组生成JSON模式。但它没有验证多个记录,如所有类似类型的元组的循环。 下面是json样本。

{
  "Data":
   [
      [ 100, "Test", 2.5 ],
      [ 101, "Test1", 3.5]
   ]
}

我已使用网站jsonschema.net生成架构,如下所示

{

  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net",
  "type": "object",
  "properties": {
    "Data": {
      "id": "http://jsonschema.net/Data",
      "type": "array",
      "items": [
        {
          "id": "http://jsonschema.net/Data/0",
          "type": "array",
          "items": [
            {
              "id": "http://jsonschema.net/Data/0/0",
              "type": "integer"
            },
            {
              "id": "http://jsonschema.net/Data/0/1",
              "type": "string"
            },
            {
              "id": "http://jsonschema.net/Data/0/2",
              "type": "number"
            }
          ],
          "required": [
            "0",
            "1",
            "2"
          ]
        },
        {
          "id": "http://jsonschema.net/Data/1",
          "type": "array",
          "items": [
            {
              "id": "http://jsonschema.net/Data/1/0",
              "type": "integer"
            },
            {
              "id": "http://jsonschema.net/Data/1/1",
              "type": "string"
            },
            {
              "id": "http://jsonschema.net/Data/1/2",
              "type": "number"
            }
          ]
        }
      ],
      "required": [
        "0",
        "1"
      ]
    }
  },
  "required": [
    "Data"
  ]
}

如果你看到,它正在为每个相似类型的元组创建模式。请帮我创建一个模式,以通用的方式验证每个元组。元组数可能会有所不同。

2 个答案:

答案 0 :(得分:7)

如果您希望内部数组包含所有相同类型的项目,则可以使用对象而不是数组。以下架构验证了您的示例:

{
    "type" : "object",
    "properties" : {
        "Data" : {
            "type" : "array",
            "items" : {
                "type" : "array",
                "items" : [{
                        "type" : "integer"
                    }, {
                        "type" : "string"
                    }, {
                        "type" : "number"
                    }
                ]
            }
        }
    }
}

我测试了它here

答案 1 :(得分:0)

  {
  "Table1": {
    "Data": [
      [
        100,
        "Test",
        2.5
      ],
      [
        101,
        "Test1",
        5.5
      ]
    ]
  }
}

以上是json& amp;其架构如下

    {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net",
  "type": "object",
  "properties": {
    "Table1": {
      "id": "http://jsonschema.net/Table1",
      "type": "object",
      "properties": {
        "Data": {
          "id": "http://jsonschema.net/Table1/Data",
          "type": "array",
          "items": {
            "type": "array",
            "items": [
              {
                "id": "http://jsonschema.net/Table1/Data/0/0",
                "type": "integer"
              },
              {
                "id": "http://jsonschema.net/Table1/Data/0/1",
                "type": "string"
              },
              {
                "id": "http://jsonschema.net/Table1/Data/0/2",
                "type": "number"
              }
            ],
            "additionalItems": false,
            "required": [
              "0",
              "1",
              "2"
            ]
          }
        }
      },
      "required": [
        "Data"
      ]
    }
  }
}

此架构适用于所有Data行,但其必需属性无法以某种方式工作。虽然我期待所有3列数据。它也接受带有1或​​2列的行。 如果有人有任何想法。请纠正我。

[ 101 ], [ 101, "TEST3" ]

也是有效的数据记录。