簡體   English   中英

如何為Tree結構創建JSON Schema?

[英]How to create JSON Schema for Tree structure?

我有一個樹結構,我想創建一個JSON模式。

班級結構

class Node {

   String id;
   List<Node> children = new ArrayList<>();

}

到目前為止的JSON模式:

{
  "name": "node",
  "type": "object",
  "properties": {
     "id": {
        "type": "string",
        "description": "The node id",
        "required": true
     }
     "children": {
        "type": "array",
        "items": {
           //The items of array should be node ?               
        }
     }
  }
}

我的問題是我不知道如何描述JSON中數組的內容"items"

在此先感謝您的回答。

只需使用JSON引用指向模式本身:

{
  "type": "object",
  "required": [ "id" ],
  "properties": {
     "id": {
        "type": "string",
        "description": "The node id"
     },
     "children": {
        "type": "array",
        "items": { "$ref": "#" }
     }
  }
}

# JSON參考意味着在本質上“文件本身”。 因此,這允許您定義遞歸模式,如此處所示。

注意:重寫以使其符合草案v4。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM