-1

I have query result like this :

DEPT_NAME DEPT_R_IDX DEPT_IDX DEPT_LEVEL
root 0 1 0
dept_0 1 2 1
dept_1 1 3 1
dept_1_0 3 4 2
dept_1_1 3 5 2
dept_2 1 6 1
dept_2_0 6 7 2
dept_2_0_1 7 8 3
  • DEPT_IDX is PRIMARY KEY, DEPT_LEVEL is TREE DEPTH, DEPT_R_IDX is parent's DEPT_IDX

I stored this data in Java's List.

List<HashMap<String, String>> DEPT_LIST;

I want convert this List to Json like this :

[
    {
        "title": "root",
        "key": "1",
        "expanded": true,
        "folder": true,
        "children": [
            {
                "key": "2",
                "title": "dept_0",
                "expanded": true,
                "folder": true
            },
            {
                "key": "3",
                "title": "dept_1",
                "expanded": true,
                "folder": true,
                "children": [
                    {
                        "key": "4",
                        "title": "dept_1_0",
                        "expanded": true,
                        "folder": true
                    },
                    {
                        "key": "5",
                        "title": "dept_1_1",
                        "expanded": true,
                        "folder": true
                    }
                ]
            },
            {
                "key": "6",
                "title": "dept_2",
                "expanded": true,
                "folder": true,
                "children": [
                    {
                        "key": "7",
                        "title": "dept_2_0",
                        "expanded": true,
                        "folder": true,
                        "children": [
                            {
                                "key": "8",
                                "title": "dept_2_1",
                                "expanded": true,
                                "folder": true
                            }
                        ]
                    }
                ]
            }

        ]
    }
]

result tree using this json data(using fancytree)

1

i tried this in browser side, but it's too low performance to make json structure

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • Usually we get POs(Persistant object) from database, and use json frameworks like jackson return the POs to frontend, we use @JsonProperty on field of PO to customize how to format one field – Hi computer Jan 31 '23 at 06:32
  • I don\`t think it is a good idea that you save the data in a List> – Hi computer Jan 31 '23 at 06:33

1 Answers1

0

Instead of storing your data in a List<HashMap<String, String>>, store them in a List<Node> where class Node is something like this:

public class Node {
    private String title;
    private String key;
    private boolean expanded;
    private boolean folder;
    private List<Node> children;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }

    public boolean isFolder() {
        return folder;
    }

    public void setFolder(boolean folder) {
        this.folder = folder;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }
}

Then use Jackson or Gson to generate the corresponding Json.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24