简体   繁体   English

Java-树结构到JSON对象

[英]Java - Tree Structure to JSON object

I have a tree structure modeled on the following template: https://github.com/gt4dev/yet-another-tree-structure/blob/master/java/src/com/tree/TreeNode.java 我有一个基于以下模板的树结构: https : //github.com/gt4dev/yet-another-tree-structure/blob/master/java/src/com/tree/TreeNode.java

I'd like to dump the entire tree into a JSON object. 我想将整个树转储到JSON对象中。 I have looked into using Jackson but I have a hard time finding adequate usage examples for this particular task. 我已经研究过使用Jackson,但是很难找到适合此特定任务的用法示例。

EDIT: I should have been clearer that what I'm looking for is a JSON file that has the same structure as the original tree, as such what I'm looking for is something like: 编辑:我应该更清楚我正在寻找的是一个与原始树具有相同结构的JSON文件,因此我正在寻找的是这样的:

{
    "root": data {
        "child 1": "data child 1",
        "child 2": "data child 2" { 
              "child of child 2": "data..."

and so on... 等等...

JSON is a string format - a linear one. JSON是一种字符串格式-一种线性格式。

A tree is not a linear data type. 树不是线性数据类型。

With these, you need to decide on how a tree looks like as a string. 有了这些,您需要确定一棵树看起来像一个字符串的样子。

You can try: 你可以试试:

  • Make a tuple string: "A(B(E,F),C(G,H))" 创建一个元组字符串: "A(B(E,F),C(G,H))"
  • Traversing a tree and making an array out of it: ["A", "B", C"] 遍历一棵树并从中构成一个数组: ["A", "B", C"]
  • Creating a table/map of parent-children pairings: [{"node":"A","children":["B","C"]},{"node":"B","children":["E","F"]}] 创建父子配对的表/映射: [{"node":"A","children":["B","C"]},{"node":"B","children":["E","F"]}]
  • ...or maybe something else, be creative! ...或者其他一些东西, 要有创造力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM