首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从深层递归父子关系构建JSON

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

Stack Overflow用户
提问于 2016-10-17 16:06:06
回答 1查看 826关注 0票数 0

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

代码语言:javascript
运行
复制
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 -

代码语言:javascript
运行
复制
{
  "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的孩子的孩子,但是深度递归没有发生。

代码语言:javascript
运行
复制
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;
    }
EN

Stack Overflow用户

发布于 2016-10-17 16:25:48

您错过了对子对象的递归调用:

代码语言:javascript
运行
复制
        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());
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40081322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档