2014-03-06 65 views
3

我有一個樹結構,我想創建一個JSON模式。如何爲樹結構創建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"

在此先感謝您的答覆。

+0

你可以做的一件事,你可以創建一個JavaScript對象作爲樹結構,然後將其字符串化爲 –

+0

我需要這樣做的文件目的。 –

回答

7

只需使用一個JSON參考點回模式本身:

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

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

注意:重寫,以便它符合草案v4。

+1

感謝您的回答。 –