简体   繁体   中英

Build a json for tree structure in java

I have 3 Classes

  • ClassA contains a_id , a_name
  • ClassB contains b_id , b_name
  • ClassC contains foreign keys of ClassA and ClassB ie, c_id , c_name , fk_a_id , fk_b_id

i want to create a tree from ClassC as

a_name1        //.....parent
   - b_name1
   - b_name2
a_name2
   - b_name1  //...... childs
   - b_name2

i want the json as

   [  
   {  
      "id":a_id1,
      "name":"a_name1",
      "parent":0
   },
   {  
      "id":b_id1",
      "name":"b_name1",
      "parent":a_id1
   },
   {  
      "id":b_id1,
      "name":"b_name2",
      "parent":a_id1
   },
   {  
      "id":a_id2,
      "name":"a_name2",
      "parent":0
   },
   {  
      "id":b_id1,
      "name":"b_name1",
      "parent":a_id2
   },
   {  
      "id":b_id1,
      "name":"b_name2",
      "parent":a_id2
   }
]

How can i achieve the above json using java and hibernate

My Research

public List<ClassCTreeDto> unique() {
        Session session= getSession();
        Criteria crit = session.createCriteria(ClassC.class);


        List<ClassCTreeDto> hierarchydto=new ArrayList<ClassCTreeDto>();
            List<ClassC> cmList  = crit.list();

            for(ClassCs :cmList){
                ClassCTreeDto tDto= new ClassCTreeDto();
                tDto.setId(s.getFkId().getId());
                tDto.setName(s.getFkId().getIdName());
                tDto.setParent(s.getFkId().getGradeId());

                if(s.getFkId()==null)
                {
                    tDto.setId((long) 0);
                }
                else
                {
                    tDto.setId(s.getFkId().getGradeId());

                }
            hierarchydto.add(tDto);
            }


            return hierarchydto;
        }

but iam getting json as,

[{"id":1,"name":"X","parent":1},
{"id":1,"name":"X","parent":1},
{"id":2,"name":"IX","parent":2},
{"id":2,"name":"IX","parent":2}]

I Just solved the issue

public Set<ClassCTreeDto> unique() {

            long parent = 0;
            long div_id = 10;

            List<ClassCTreeDto> treestructure = new ArrayList<ClassCTreeDto>();
            List<ClassCTreeDto> treestructure2 = new ArrayList<ClassCTreeDto>();
            Set<ClassCTreeDto> all = new HashSet<ClassCTreeDto>();

            List<ClassC> list = getAll();

            for (ClassC ClassC: list) {
                ClassCTreeDto tree = new ClassCTreeDto();

                tree.setParent(parent);
                tree.setId(ClassC.getFkId().getId());
                tree.setName(ClassC.getFkId().getIdName());

                if (containsLocation(treestructure, tree.getIdName())) {
                    treestructure.remove(tree);

                }

                else {
                    treestructure.add(tree);
                }

            }
            for (ClassC classC : list) {

                ClassCTreeDto tree2 = new ClassCTreeDto ();

                tree2.setParent(classC.getFkId().getId());
                tree2.setId(div_id);
                tree2.setName(classC.getFkDId().getDName());

                treestructure2.add(tree2);

            }

            all.addAll(treestructure);
            all.addAll(treestructure2);

            return all;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM