在java中为树结构构建一个json

时间:2017-07-20 06:41:52

标签: java json hibernate

我有3个班级

  • ClassA 包含a_id , a_name
  • ClassB 包含b_id , b_name
  • ClassC 包含ClassAClassB外键,即c_id , c_name , fk_a_id , fk_b_id

我想从 ClassC 创建一个树

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

我希望json为

   [  
   {  
      "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
   }
]

如何使用java和hibernate实现上述json

我的研究

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;
        }

但是我得到了json,

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

1 个答案:

答案 0 :(得分:2)

我刚刚解决了这个问题

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;
        }