I have a tree with nodes like this:

我有一个像这样的节点的树:

-Root
|-Node 1
|-Node 2
  |-Node 2.1

one of these nodes are representet by one class

其中一个节点由一个类代表

class Node {
    Integer id;
    String name;
    String route;
}

instances of the shown nodes are saved like

所示节点的实例保存为

{id: 1, name: "Root", route:"1"}
{id: 2, name: "Node 1", route: "1/2"}
{id: 3, name: "Node 2", route: "1/3"}
{id: 4, name: "Node 2.1", route: "1/3/4"}

the question is: How can i get from a list of nodes to json representing the tree state, e.g.:

问题是:如何从节点列表到表示树状态的json,例如:

[{
"property": {
    "name": "Root",
    "id": "1",
    "route": "1"
},
"children": [{
    "property": {
        "name": "Node 1",
        "id": "2",
        "route": "1/2"
    },
    "property": {
        "name": "Node 2",
        "id": "3",
        "route": "1/3"
    },
    "children": [{
        "property": {
            "name": "Node 3",
            "id": "4",
            "route": "1/3/4"
        }
    }]
}]
}]

i need exactly this json structure

我需要这个json结构

all this stuff have to be done in java. i tried to iterate over my list of nodes and built the json object, but i have problems to get the json structure from the route field of the nodes

所有这些都必须在java中完成。我试图遍历我的节点列表并构建json对象,但我有问题从节点的路由字段获取json结构

i'm able to use a json library the node class is unchangable

我能够使用json库,节点类是不可更改的

EDIT: This format is quite strange, but needed. The "properties" are the nodes, if the "propertie" has children, they are not placed IN the propertie, ether AFTER the propertie... i think thats not quite sensefull but i cant change it

编辑:这种格式很奇怪,但需要。 “属性”是节点,如果“属性”有孩子,他们没有被放置在属性,以后属性......我认为这不是很有意义,但我无法改变它

2 个解决方案

#1


4

You could try an algorithm something like this:

您可以尝试这样的算法:

public JSONObject toJSON(Node node, List<Node> others) {
    JSONObject json = new JSONObject();
    json.put("id", node.id); // and so on
    ...
    List children = new ArrayList<JSONObject>();
    for(Node subnode : others) {
        if(isChildOf(subnode, node)) {
            others.remove(subnode);
            children.add(toJSON(subnode, others));
        }
    }
    json.put("children", children);
    return json;
}

You are modifying a list as you iterate over it, and as recursive calls also iterate over it. This might be hairy, but try it. There are ways around it if it fails.

您在迭代时修改列表,并且递归调用也会迭代它。这可能是毛茸茸的,但试试吧。如果失败,有办法解决它。

isChildOf() is the missing piece. That's some fairly basic string manipulation, to see whether subnode.path starts with node.id

isChildOf()是缺失的部分。这是一些相当基本的字符串操作,以查看subnode.path是否以node.id开头

Edit: actually this doesn't create the same structure as in your question. But I can't make much sense of the structure in your question. However something very similar to this algorithm will produce what you want. The principle is sound.

编辑:实际上这不会创建与您的问题相同的结构。但我无法理解你问题中的结构。然而,与此算法非常相似的东西将产生您想要的效果。原则是健全的。

更多相关文章

  1. 包含带标记的值的XML属性文件
  2. JAVA中的反射只获取属性的get方法
  3. 如何在Spring中读取具有相同键的多个属性?
  4. 为泛型类的泛型方法的属性赋值 - Java
  5. 如何在Spring Data(JPA)派生查询中按多个属性排序?

随机推荐

  1. 设计模式使用场景、优缺点汇总
  2. List集合就这么简单【源码剖析】
  3. 每个人理解的设计模式应该都不太一样
  4. 《面向模式的软件体系结构 - 卷1 模式系
  5. 单例模式在开源代码中的应用
  6. 运维转管理的成长之路
  7. 工厂模式在开源代码中的应用
  8. String常量地址变动引起的诡异结果
  9. 建造者模式和原型模式在开源代码中的应用
  10. 最想放弃的时候,离成功最近