小编Ped*_*dro的帖子

用Jackson递归地为jsTree构建一个JSON字符串

我一直在尝试使用Jackson库(v.1.7.4,这是我可以用于此项目的唯一一个)以jsTree(https://www.jstree)接受的格式在Java中构建一个JSON字符串.com/docs/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);
Run Code Online (Sandbox Code Playgroud)

我希望我的String是:

 {text: "root", children: [{text:"A", children:[{text:"C", children: []}]}, {text:"B", children: [{text: "D", children: []}, {text:"E", children:[]}]}] }
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用Jackson的Tree Model构建JSON字符串.到目前为止我的代码看起来像这样:

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

    ObjectMapper mapper = …
Run Code Online (Sandbox Code Playgroud)

java recursion json jackson jstree

6
推荐指数
1
解决办法
1383
查看次数

标签 统计

jackson ×1

java ×1

json ×1

jstree ×1

recursion ×1