gpt4 book ai didi

java - 如何获取 json 中每个对象的级别,其中对象可以有任意数量的相同对象类型的子对象

转载 作者:搜寻专家 更新时间:2023-11-01 03:17:12 24 4
gpt4 key购买 nike

我有一个员工级别结构,如下图所示 enter image description here

这个结构是在 json 的帮助下填充和存储的,就像这样

 {
"name": "Lao Lao",
"title": "general manager",
"children": [
{
"name": "Bo Miao",
"title": "department manager",
"children": [
{
"name": "Li Jing",
"title": "senior engineer"
},
{
"name": "Li Xin",
"title": "senior engineer",
"children": [
{
"name": "To To",
"title": "engineer"
},
{
"name": "Fei Fei",
"title": "engineer"
},
{
"name": "Xuan Xuan",
"title": "engineer"
}
]
}
]
},
{
"name": "Su Miao",
"title": "department manager",
"children": [
{
"name": "Pang Pang",
"title": "senior engineer"
},
{
"name": "Hei Hei",
"title": "senior engineer",
"children": [
{
"name": "Xiang Xiang",
"title": "UE engineer"
},
{
"name": "Dan Dan",
"title": "engineer"
},
{
"name": "Zai Zai",
"title": "engineer"
}
]
}
]
}
]
}

我想解析这个 Json 以获取所有具有这样的级别和父级名称的对象

{name =  Lao lao , parent = null , level = 1 }
{name = Bao Miao , parent = Lao lao , level = 2 }
..................................................
{name = Li Jing , parent = Bao Miao , level = 3 }

我们如何在 java 的帮助下解析它?如果有任何具有此类功能的库,请告诉我。

最佳答案

按如下方式实现模型定义。还将 level 和 parentName 放在 Model 类中。

class Employee{
String name;
String title;
Employee children[];
int level;
String parentName;

@Override
public String toString(){
return "{name = "+name+" , parent = "+parentName+ ", level = "+level+ " }";
}
}

使用 GSON API 解析 json 数据。

Employee e= new Gson().fromJson(new JsonReader(new FileReader("file.json")), Employee.class);

这是适合您的完整程序。在花了一个小时之后,我终于能够为您编写所有代码。到目前为止工作正常:)

import java.io.FileReader;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

public class ParseJson {

public static void main(String a[]) {
Gson g = new Gson();
try {
Employee e = g.fromJson(new JsonReader(new FileReader("file.json")), Employee.class);

parseEmployees(e);
printEmployee(e);
} catch (Exception e1) {
e1.printStackTrace();
}

}

private static void parseEmployees(Employee e) {
setParentAndLevel(e, 1, null);
}

private static void setParentAndLevel(Employee e, int lvl, String parent) {
e.level = lvl;
e.parentName = parent;
if (e.children != null && e.children.length > 0) {
lvl++;
for (Employee emp : e.children) {
setParentAndLevel(emp, lvl, e.name);
}
}
}

public static void printEmployee(Employee e){
System.out.println(e);
if (e.children != null && e.children.length > 0) {
for (Employee emp : e.children) {
printEmployee(emp);
}
}else{
return ;
}
}
}

class Employee {
String name;
String title;
Employee children[];
int level;
String parentName;

@Override
public String toString() {
return "{name = " + name + " , parent = " + parentName + ", level = " + level + " }";
}
}

输出:

{name =  Lao Lao , parent = null, level = 1 }
{name = Bo Miao , parent = Lao Lao, level = 2 }
{name = Li Jing , parent = Bo Miao, level = 3 }
{name = Li Xin , parent = Bo Miao, level = 3 }
{name = To To , parent = Li Xin, level = 4 }
{name = Fei Fei , parent = Li Xin, level = 4 }
{name = Xuan Xuan , parent = Li Xin, level = 4 }
{name = Su Miao , parent = Lao Lao, level = 2 }
{name = Pang Pang , parent = Su Miao, level = 3 }
{name = Hei Hei , parent = Su Miao, level = 3 }
{name = Xiang Xiang , parent = Hei Hei, level = 4 }
{name = Dan Dan , parent = Hei Hei, level = 4 }
{name = Zai Zai , parent = Hei Hei, level = 4 }

关于java - 如何获取 json 中每个对象的级别,其中对象可以有任意数量的相同对象类型的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45767355/

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