gpt4 book ai didi

java - 从深度递归父子关系构建 JSON

转载 作者:行者123 更新时间:2023-12-01 09:22:29 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 从深度递归父子关系构建 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40081322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com