gpt4 book ai didi

java - java中将JSON数据结构遍历成树

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:26 27 4
gpt4 key购买 nike

我需要从 JSON 构建一棵树。 JSON 结构读取,对于每个 restProjectLevel :restProjectLevels 我有一个 id = 级别。序列号 = 根或子。对于每个restProject:restProjects,我都有一个 id = projectId 和一个projectIdHook = 附加的projectId。

我认为 JSON 数据可以最好地解释它。

{
"id": 3,
"description": "New Project Plan",
"restProjectLevels": [
{
"id": 19,
"sequenceNr": 0,
"restProjects": [
{
"id": 28,
"projectName": "Project A",
"description": "",
"projectLevelId": 19,
"projectIdHook": 0,
"restProjectProcesses": []
},
{
"id": 29,
"projectName": "Project B",
"description": "",
"projectLevelId": 19,
"projectIdHook": 0,
"restProjectProcesses": []
},
{
"id": 30,
"projectName": "Project C",
"description": "",
"projectLevelId": 19,
"projectIdHook": 0,
"restProjectProcesses": []
},
{
"id": 41,
"projectName": "New",
"description": "",
"projectLevelId": 19,
"projectIdHook": 0,
"restProjectProcesses": []
}
]
},
{
"id": 20,
"sequenceNr": 1,
"restProjects": [
{
"id": 31,
"projectName": "Project A.1",
"description": "",
"projectLevelId": 20,
"projectIdHook": 28,
"restProjectProcesses": []
},
{
"id": 33,
"projectName": "Project B.1",
"description": "",
"projectLevelId": 20,
"projectIdHook": 29,
"restProjectProcesses": []
},
{
"id": 35,
"projectName": "Project C.1",
"description": "",
"projectLevelId": 20,
"projectIdHook": 30,
"restProjectProcesses": []
}
]
},
{
"id": 21,
"sequenceNr": 2,
"restProjects": [
{
"id": 32,
"projectName": "Project A.2",
"description": "",
"projectLevelId": 21,
"projectIdHook": 31,
"restProjectProcesses": []
},
{
"id": 36,
"projectName": "Project C.2",
"description": "",
"projectLevelId": 21,
"projectIdHook": 35,
"restProjectProcesses": []
}
]
},
{
"id": 22,
"sequenceNr": 3,
"restProjects": [
{
"id": 34,
"projectName": "Projet B.4",
"description": "",
"projectLevelId": 22,
"projectIdHook": 33,
"restProjectProcesses": []
},
{
"id": 37,
"projectName": "Project C.3",
"description": "",
"projectLevelId": 22,
"projectIdHook": 36,
"restProjectProcesses": []
}
]
},
{
"id": 23,
"sequenceNr": 4,
"restProjects": []
}
]
}

最佳答案

好的解决了这个问题!不是最优雅的解决方案,但它有效......

    for(RestProjectLevel projectLevel: projectLevelList) {
if(projectLevel.getSequenceNr() == 0) {
for(RestProject project : projectLevel.getRestProjects()) {
restParentNode = new RestParentNode();
traverse(sortedRestProjects, project, restParentNode);
restTree.nodes.add(restParentNode);
}
}
}

private void traverse(List<RestProject> restProjects, RestProject project, RestParentNode restParentNode) {
for (RestProject restProject : restProjects) {
if(project.getId() == restProject.getProjectIdHook() || project.getId() == restProject.getId()) {

String projectName = restProject.getProjectName();

if(hasNode(projectName)) {
continue;
} else {
RestNode restNode = new RestNode();
restNode.nodes.add(restProject);
restParentNode.nodes.add(restNode);
traverse(restProjects, restProject, restParentNode);
}
}
}
}

谢谢!

关于java - java中将JSON数据结构遍历成树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195063/

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