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/

相关文章:

jQuery 从 JSON 解析 HTML

arrays - 如何处理 Angular 7 中数据不均匀的 JSON 对象?

guid - 要反序列化 JSON.NET 的空 GUID 的正确格式是什么?

c# - JSON.Net 是否保证始终以相同的方式序列化?

javascript - 带有比较运算符的 bool 表达式的类型

javascript - 如何用 Jquery/JS 交换 2 @Html.DropDownListFor

android - 使用移动数据时出现 JsonSyntaxException

javascript - 添加到 dom 的行未选择所有复选框列

javascript - 如何检查动态添加的 tealium utag 是否已提交?

c# - WebApi 无法遵守 JsonObjectAttribute 设置