在Java中构建来自Json平面数组的树结构--Android

时间:2017-02-28 15:49:44

标签: java android json

我有一个包含产品类别的json文件。

json的每个条目都有:

  • Id:唯一类别ID
  • IdParent:父节点的ID
  • ...姓名&的ImageUrl

示例日期:

[  
   {  
      "ImageUrl":"1-home_default/1",
      "Id":1,
      "IdParent":0,
      "Name":"Root"
   },
   {  
      "ImageUrl":"2-home_default/2",
      "Id":2,
      "IdParent":1,
      "Name":"Home"
   },
   {  
      "ImageUrl":"12-home_default/12",
      "Id":12,
      "IdParent":2,
      "Name":"Fruit"
   },
   {  
      "ImageUrl":"336-home_default/336",
      "Id":336,
      "IdParent":2,
      "Name":"Papers"
   },
   {  
      "ImageUrl":"33-home_default/33",
      "Id":33,
      "IdParent":2,
      "Name":"Food"
   },
   {  
      "ImageUrl":"95-home_default/95",
      "Id":95,
      "IdParent":2,
      "Name":"Meet"
   },
   {  
      "ImageUrl":"121-home_default/121",
      "Id":121,
      "IdParent":2,
      "Name":"Bolvery"
   },
   {  
      "ImageUrl":"145-home_default/145",
      "Id":145,
      "IdParent":2,
      "Name":"Milk Breakfast"
   },
   {  
      "ImageUrl":"189-home_default/189",
      "Id":189,
      "IdParent":2,
      "Name":"Food OUt"
   },
   {  
      "ImageUrl":"269-home_default/269",
      "Id":269,
      "IdParent":2,
      "Name":"Cleaning"
   },
   {  
      "ImageUrl":"305-home_default/305",
      "Id":305,
      "IdParent":2,
      "Name":"Babies"
   },
   {  
      "ImageUrl":"401-home_default/401",
      "Id":401,
      "IdParent":2,
      "Name":"Lifestyle"
   },
   {  
      "ImageUrl":"413-home_default/413",
      "Id":413,
      "IdParent":2,
      "Name":"Products"
   },
   {  
      "ImageUrl":"426-home_default/426",
      "Id":426,
      "IdParent":2,
      "Name":"Copaigns"
   },
   {  
      "ImageUrl":"23-home_default/23",
      "Id":23,
      "IdParent":12,
      "Name":"Seeds"
   },
   {  
      "ImageUrl":"344-home_default/344",
      "Id":344,
      "IdParent":336,
      "Name":"Mouth products"
   },
   {  
      "ImageUrl":"34-home_default/34",
      "Id":34,
      "IdParent":33,
      "Name":"Macarna"
   },
   {  
      "ImageUrl":"103-home_default/103",
      "Id":103,
      "IdParent":95,
      "Name":"Animals"
   },
   {  
      "ImageUrl":"127-home_default/127",
      "Id":127,
      "IdParent":121,
      "Name":"Drinks"
   },
   {  
      "ImageUrl":"146-home_default/146",
      "Id":146,
      "IdParent":145,
      "Name":"Milk"
   },
   {  
      "ImageUrl":"190-home_default/190",
      "Id":190,
      "IdParent":189,
      "Name":"Electronics"
   },
   {  
      "ImageUrl":"276-home_default/276",
      "Id":276,
      "IdParent":269,
      "Name":"Cleaning2"
   },
   {  
      "ImageUrl":"310-home_default/310",
      "Id":310,
      "IdParent":305,
      "Name":"Kids Cleaning"
   },
   {  
      "ImageUrl":"402-home_default/402",
      "Id":402,
      "IdParent":401,
      "Name":"Traveling"
   }
]

我正在尝试将此Json文件填充到树结构中,如下所示:

public class Tree<T> {
    private Node<T> root;

    public Tree(T rootData) {
        root = new Node<T>();
        root.data = rootData;
        root.children = new ArrayList<Node<T>>();
    }

    public static class Node<T> {
        private T data;
        private Node<T> parent;
        private List<Node<T>> children;
    }
}

因此,当用户点击节点时,如果它有子节点,它将列出子节点,否则,它将打开该类别的产品。

我已将Json转换为List<Category>

如何将List<Category>转换为Tree

1 个答案:

答案 0 :(得分:0)

基于我已经找到here的答案,我已经用Java完成了这样的操作:

我的映射对象:

public class MyObject {
    public Long objectId;
    public String objectName;
    public Long parentObjectId;

    public MyObject(Long objectId, String objectName, Long parentObjectId) {
        this.objectId = objectId;
        this.objectName = objectName;
        this.parentObjectId = parentObjectId;
    }
}

Node类:

public class Node {
    public List<Node> Children = new ArrayList<>();
    public Node Parent;
    public MyObject Source;

    public MyObject getSourceObject() {
        return Source;
    }
    public void setSourceObject(MyObject Source) {
        Source = Source;
    }
    public Node getParent() {
        return Parent;
    }
    public void setParent(Node parent) {
        Parent = parent;
    }
}

如何构建树:

public List<Node> BuildTreeAndGetRoots(List<MyObject> actualObjects) {
    Map<Long, Node> lookup = new HashMap<>();
    List rootNodes = new ArrayList<Node>();

    for (MyObject object : actualObjects) {
        // add us to lookup
        Node ourNode;
        if (lookup.containsKey(object.objectId)) {   // was already found as a parent - register the actual object
            ourNode = lookup.get(object.objectId);
            ourNode.Source = object;
        } else {
            ourNode = new Node();
            ourNode.Source = object;

            lookup.put(object.objectId, ourNode);
        }

        // hook into parent
        if (object.parentObjectId == null || object.parentObjectId.equals(object.objectId)) {   // is a root node
            rootNodes.add(ourNode);
        } else {   // is a child row - so we have a parent
            Node parentNode;
            if (!lookup.containsKey(object.parentObjectId)) {   // unknown parent, construct preliminary parent
                parentNode = new Node();
                lookup.put(object.parentObjectId, parentNode);
            } else {
                parentNode = lookup.get(object.parentObjectId);
            }
            parentNode.Children.add(ourNode);
            ourNode.Parent = parentNode;
        }
    }

    return rootNodes;
}