javascript - 元组数组的 JSON 模式

标签 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"
  ]
}

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

最佳答案

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

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

我测试过了here .

关于javascript - 元组数组的 JSON 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334376/

相关文章:

c# - 如何在JArray上做Top n?

c# - 将 C# 类转换为具有自定义结构的 Json

.net - 如何验证 JsonSchema 类型属性中的多个 $ref

javascript - 尝试读取这个 Javascript 循环

JavaScript HTML - 按内容优化页面上的项目

javascript - Thorax.js Collection View 不渲染模型属性

javascript - 对于转换为 `label` 和 `value` 的 JSON 流,jQuery 自动完成结果框不显示

c# - 将 JSON 转换为对象列表

JSON.NET 添加到 JArray

JavaScript 正则表达式不起作用