【问题标题】:JSON Schema for Array of tuples元组数组的 JSON 模式
【发布时间】:2015-12-17 12:15:40
【问题描述】:

提前致谢。

我是 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"
  ]
}

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

【问题讨论】:

    标签: javascript json json.net jsonschema


    【解决方案1】:

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

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

    我已经测试过了here。

    【讨论】:

    • 谢谢 Jruizaranguren .. 我已经测试过了。如果可行,我将在我的解决方案中尝试。但我认为它会帮助我。非常感谢。
    • 您好 Jruizaranguren,您的解决方案运行良好。我遇到了下面提到的另一个问题。 REQUIRED 不工作的数组元素。我已将问题描述如下。提前感谢您的帮助。
    • 这不应该是一个新问题吗? BTW REQUIRED 是一个仅适用于对象元素的关键字。
    • 在同一个线程中添加了这个问题,只是为了与旧帖子相关联。我将添加新问题。感谢您的帮助。
    【解决方案2】:
      {
      "Table1": {
        "Data": [
          [
            100,
            "Test",
            2.5
          ],
          [
            101,
            "Test1",
            5.5
          ]
        ]
      }
    }
    

    以上是示例 json,其架构如下

        {
      "$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"
          ]
        }
      }
    }
    

    此架构适用于所有数据行,但它所需的属性无法正常工作。尽管我期待所有 3 列数据。它也接受具有 1 或 2 列的行。 如果有人有任何想法。请纠正我。

    [ 101 ], [ 101, "TEST3" ]
    

    也是预期之外的有效数据记录。

    【讨论】:

      【解决方案3】:

      JSON schema 具有用于元组的新语法,之前建议的解决方案现在可以这样编写:

      {
        "type": "object",
        "properties": {
          "Data": {
            "type": "array",
            "items": [
              {
                "type": "array",
                "prefixItems": [
                  { "type": "integer" },
                  { "type": "string" },
                  { "type": "integer" }
                ],
                "minItems": 3,
                "items": false    
              }
            ]
          }
        },
        "required": [
          "Data"
        ]
      }
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 2020-02-09
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多