2015-11-09 85 views
6

我一直在試圖建立一個JSON字符串在Java中使用傑克遜庫(v.1.7.4,這是我可以用於該項目的唯一一個)到格式由jsTree接受(https://www.jstree.com/docs/json/)。我只關心「文字」和「兒童」的屬性。問題是,我沒有得到一個工作的遞歸方法來做到這一點。遞歸構建一個JSON字符串與jsTree與傑克遜

如果我有一個簡單的樹像這樣的:

Tree<String> tree = new Tree<String>(); 
    Node<String> rootNode = new Node<String>("root"); 
    Node<String> nodeA = new Node<String>("A"); 
    Node<String> nodeB = new Node<String>("B"); 
    Node<String> nodeC = new Node<String>("C"); 
    Node<String> nodeD = new Node<String>("D"); 
    Node<String> nodeE = new Node<String>("E"); 

    rootNode.addChild(nodeA); 
    rootNode.addChild(nodeB); 
    nodeA.addChild(nodeC); 
    nodeB.addChild(nodeD); 
    nodeB.addChild(nodeE); 

    tree.setRootElement(rootNode); 

我倒是希望我的字符串是:

{text: "root", children: [{text:"A", children:[{text:"C", children: []}]}, {text:"B", children: [{text: "D", children: []}, {text:"E", children:[]}]}] } 

我正嘗試建立使用樹模型的JSON字符串來自傑克遜。我的代碼到目前爲止看起來像這樣:

public String generateJSONfromTree(Tree<String> tree) throws IOException{ 
    String json = ""; 

    ObjectMapper mapper = new ObjectMapper(); 
    JsonFactory factory = new JsonFactory(); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); // buffer to write to string later 
    JsonGenerator generator = factory.createJsonGenerator(out, JsonEncoding.UTF8); 

    JsonNode rootNode = mapper.createObjectNode(); 
    JsonNode coreNode = mapper.createObjectNode();   

    JsonNode dataNode = (ArrayNode)generateJSON(tree.getRootElement()); // the tree nodes 

    // assembly arrays and objects 
    ((ObjectNode)coreNode).put("data", dataNode); 
    ((ObjectNode)rootNode).put("core", coreNode);  
    mapper.writeTree(generator, rootNode); 

    json = out.toString(); 
    return json; 
} 

public ArrayNode generateJSON(Node<String> node, ObjectNode obN, ArrayNode arrN){ 
    // stop condition ? 
    if(node.getChildren().isEmpty()){ 
     arrN.add(obN); 
     return arrN; 
    } 

    obN.put("text", node.getData()); 
    for (Node<String> child : node.getChildren()){ 

     // recursively call on child nodes passing the current object node 
     obN.put("children", generateJSON(child, obN, arrN)); 
    } 

} 

我嘗試了一些變化,但迄今沒有成功。我知道答案可能比我嘗試的更簡單,但我被卡住了。也許停止條件不合適或邏輯本身(我的想法是在下一次調用時嘗試重用ObjectNode和ArrayNode對象,以便將「children」元素(來自json)插入到下一個子節點上樹,所以它會倒退,但最終我得到空變量)。

我的樹和節點類是基於以下幾點:http://sujitpal.blogspot.com.br/2006/05/java-data-structure-generic-tree.html

回答

2

不是最好的方法,但它能夠完成任務:

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.util.Iterator; 

import com.fasterxml.jackson.core.JsonEncoding; 
import com.fasterxml.jackson.core.JsonFactory; 
import com.fasterxml.jackson.core.JsonGenerator; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.node.ArrayNode; 
import com.fasterxml.jackson.databind.node.ObjectNode; 

public class TreeApp { 

    public String generateJSONfromTree(Tree<String> tree) throws IOException { 
     ObjectMapper mapper = new ObjectMapper(); 
     JsonFactory factory = new JsonFactory(); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); // buffer to write to string later 
     JsonGenerator generator = factory.createJsonGenerator(out, JsonEncoding.UTF8); 

     ObjectNode rootNode = generateJSON(tree.getRootElement(), mapper.createObjectNode()); 
     mapper.writeTree(generator, rootNode); 

     return out.toString(); 
    } 

    public ObjectNode generateJSON(Node<String> node, ObjectNode obN) { 
     if (node == null) { 
      return obN; 
     } 

     obN.put("text", node.getData()); 

     ArrayNode childN = obN.arrayNode(); 
     obN.set("children", childN);   
     if (node.getChildren() == null || node.getChildren().isEmpty()) { 
      return obN; 
     } 

     Iterator<Node<String>> it = node.getChildren().iterator(); 
     while (it.hasNext()) { 
      childN.add(generateJSON(it.next(), new ObjectMapper().createObjectNode())); 
     } 
     return obN; 
    } 

    public static void main(String[] args) throws IOException { 
     Tree<String> tree = new Tree<String>(); 
     Node<String> rootNode = new Node<String>("root"); 
     Node<String> nodeA = new Node<String>("A"); 
     Node<String> nodeB = new Node<String>("B"); 
     Node<String> nodeC = new Node<String>("C"); 
     Node<String> nodeD = new Node<String>("D"); 
     Node<String> nodeE = new Node<String>("E"); 

     rootNode.addChild(nodeA); 
     rootNode.addChild(nodeB); 
     nodeA.addChild(nodeC); 
     nodeB.addChild(nodeD); 
     nodeB.addChild(nodeE); 

     tree.setRootElement(rootNode); 

     System.out.println(new TreeApp().generateJSONfromTree(tree)); 
    } 
}