从深层递归父子关系构建JSON

时间:2016-10-17 08:06:06

标签: java json postgresql recursion

我在postgres数据库中有以下记录。 parent_pk与父子关系中的pk相关。 pk = 1是所有孩子的直接和间接的父母。

pk             name             type            parent_pk
---            ----             ----            ---------
1              hnumber101       house           1
2              hnumber201       house           1
791            dodge_charger    vehicle         1
801            mustang          vehicle         791
595020         civic            vehicle         2
10077661099    john             user            10046725614
10046725614    mesto            dev             1
801            shen             house           791
44444444       crep             house           10046725614
22222222       keper            user            10046725614
11111111       show             house           10046725614
84257651       shen             house           801
11             lemp             house           2

我想用以下格式生成上面的json -

{
  "children" : [
    { "pk" : "1", "name" : "hnumber101", "children" : [
      { "pk" : "10046725614", "name" : "mesto", "children" : [
          { "pk" : "10077661099", "name" : "john", "children" : [] },
          { "pk" : "44444444", "name" : "crep", "children" : [] },
          { "pk" : "22222222", "name" : "keper", "children" : [] },
          { "pk" : "11111111", "name" : "show", "children" : [] }
        ] }
      ] },
      { "pk" : "791", "name" : "dodge_charger", "children" : [
        { "pk" : "801", "name" : "mustang", "children" : [
          { "pk" : "84257651", "name" : "shen", "children" : [
          ] }
        ] },
        { "pk" : "2", "name" : "hnumber201", "children" : [
          { "pk" : "595020", "name" : "civic", "children" : [] },
          { "pk" : "11", "name" : "lemp", "children" : [] }
        ] }
      ] }
    ] }
  ]
}

使用我现在的代码,我只能得到pk = 1的孩子的孩子。 但深度递归并没有发生。

Collection<GatherEntity> gatherEntityChildren= gatherManager.findByParentGatherId(1);
getRecursiveGatherFromParent(gatherEntityChildren, gatherListParent);   

private List<Gather> getRecursiveGatherFromParent(Collection<GatherEntity> gatherEntityChildren, List<Gather> gatherListParent) throws JSONException {      


        if(gatherEntityChildren != null && gatherEntityChildren.size() > 0) {
            for (Iterator<gatherEntity> iterator = gatherEntityChildren.iterator(); iterator.hasNext();) {
                GatherEntity gatherEntity = (GatherEntity) iterator.next();

                Gather gather = getGatherFromEntity(gatherEntity);
                List<Gather> gatherChildren = populateGatherAndChild(gatherEntity);
                gather.setChildren(new HashSet<Gather>(gatherChildren));
                gatherListParent.add(gather);
            }   
        }
        return gatherListParent;
    }

    private List<Gather> populateGatherAndChild(GatherEntity gatherEntity) {
        Collection<GatherEntity> gatherEntityChildren= gatherManager.findByParentGatherId(gatherEntity.getGatherId());
        List<Gather> gatherList = gatherEntityChildren.stream().map(UserAPIImpl::getGatherFromEntity).collect(Collectors.toList());      
        return gatherList;
    }

    private static Gather getGatherFromEntity(GatherEntity gatherEntity) {
        Gather gather = new Gather();
        gather.setGatherId(gatherEntity.getGatherId());
        gather.setName(gatherEntity.getName());
        return gather;
    }

1 个答案:

答案 0 :(得分:1)

你错过了对孩子们的递归电话:

        if(gatherEntityChildren != null && gatherEntityChildren.size() > 0) {
        for (Iterator<gatherEntity> iterator = gatherEntityChildren.iterator(); iterator.hasNext();) {
            GatherEntity gatherEntity = (GatherEntity) iterator.next();

            Gather gather = getGatherFromEntity(gatherEntity);
            Collection<GatherEntity> gatherChildren = populateGatherAndChild(gatherEntity);

            List<Gather> gatherList = gatherEntityChildren.stream().map(UserAPIImpl::getGatherFromEntity).collect(Collectors.toList());
            gather.setChildren(new HashSet<Gather>(gatherList));
            gatherListParent.add(gather);

            getRecursiveGatherFromParent(gatherChildren, gatherListParent);
        }
    }
    return gatherListParent;
}

private List<GatherEntity> populateGatherAndChild(GatherEntity gatherEntity) {
    return gatherManager.findByParentGatherId(gatherEntity.getGatherId());
}
相关问题