2015-11-09 10 views
6

ジャクソンライブラリを使ってJavaでJSON文字列を作成しようとしています(v.1.7.4、このプロジェクトで使用できる唯一のものです)。 jsTree(https://www.jstree.com/docs/json/)が受け入れる形式。私は "テキスト"と "子供"のプロパティだけを気にします。問題は、私はそうするために働く再帰的な方法を得ていないということです。ジャクソンとjsTreeにJSON文字列を再帰的に構築する

私はこのような単純なツリーがある場合:

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); 

I'dは私の文字列であることを期待:

{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"要素を "insert"ツリー、それは後方に構築されるだろうが、最終的にはnull変数が得られる)。

マイツリーノードクラス

は、以下に基づいています: 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)); 
    } 
} 
関連する問題