【问题标题】:JSON Schema for tree structure树结构的 JSON 模式
【发布时间】:2016-05-10 23:38:26
【问题描述】:

我必须构建 Json 数据的树状结构。每个节点都有一个 id(一个整数,必需)、一个标签(一个字符串,可选)和一个子节点数组(可选)。你能帮我如何为这个 Json 数据编写 JSON 模式吗?我还需要在子节点中根据需要设置 Id。

{
    "Id": 1,
    "Label": "A",
    "Child": [
        {
            "Id": 2,
            "Label": "B",
            "Child": [
                {
                    "Id": 5,
                    "Label": "E"
                }, {
                    "Id": 6,
                    "Label": "E"
                }, {
                    "Id": 7,
                    "Label": "E"
                }
            ]
        }, {
            "Id": 3,
            "Label": "C"
        }, {
            "Id": 4,
            "Label": "D",
            "Child": [
                {
                    "Id": 8,
                    "Label": "H"
                }, {
                    "Id": 9,
                    "Label": "I"
                }
            ]
        }
    ]
}

enter image description here

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    这种结构的模式只需要一个节点的定义和对该节点的引用。属性Children(从Child重命名)也引用了node

    这是架构:

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "$ref": "#/definitions/node",
      "definitions": {
        "node": {
          "properties": {
            "Id": {
              "type": "integer"
            },
            "Label": {
              "type": "string"
            },
            "Children": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/node"
              }
            }
          },
          "required": [
            "Id"
          ]
        }
      }
    }
    

    【讨论】:

    • 非常感谢您的及时回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多