I have an Employee level structure like below image
This structure is populated and stored with help of json like this
{
"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"
}
]
}
]
}
]
}
I want to parse this Json to get all the objects with the level and parent name like this
{name = Lao lao , parent = null , level = 1 }
{name = Bao Miao , parent = Lao lao , level = 2 }
..................................................
{name = Li Jing , parent = Bao Miao , level = 3 }
How can we parse this with help of java?If there is any library with such functionality, please let me know.
Implement Model definition as below. Also put level and parentName in Model class.
class Employee{
String name;
String title;
Employee children[];
int level;
String parentName;
@Override
public String toString(){
return "{name = "+name+" , parent = "+parentName+ ", level = "+level+ " }";
}
}
Parse the json data using GSON API.
Employee e= new Gson().fromJson(new JsonReader(new FileReader("file.json")), Employee.class);
This is complete program for you. Finally i was able to write all the code for you after spending an hour on this. Working fine so far :)
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 + " }";
}
}
Output :
{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 }
look like I have so much time and waste my time doing this for you because it sound challenging basically I turn this into a jsonObject first then I do a bfs graph walk to find the level and parent told you it is a graph problem again there is some bug on the level but I want the community or you to fix the bug yourself
EDIT: I have fix the level bug for you already again ask me if you have any question
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
String JSON_STRING = "{\n" +
" \"name\": \"Lao Lao\",\n" +
" \"title\": \"general manager\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Bao Miao\",\n" +
" \"title\": \"department manager\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Li Jing\",\n" +
" \"title\": \"senior engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Li Xin\",\n" +
" \"title\": \"senior engineer\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"To To\",\n" +
" \"title\": \"engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Fei Fei\",\n" +
" \"title\": \"engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Xuan Xuan\",\n" +
" \"title\": \"engineer\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"name\": \"Su Miao\",\n" +
" \"title\": \"department manager\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Pang Pang\",\n" +
" \"title\": \"senior engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Hei Hei\",\n" +
" \"title\": \"senior engineer\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Xiang Xiang\",\n" +
" \"title\": \"UE engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Dan Dan\",\n" +
" \"title\": \"engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Zai Zai\",\n" +
" \"title\": \"engineer\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}] \n";
JSONObject obj = new JSONObject(JSON_STRING);
Deque<JSONObject> deque = new ArrayDeque<>();
Map<String, String> res = new HashMap<>();
int level = 1;
res.put("NULL", obj.getString("name")+ "-" + level);
deque.add(obj);
Map<String, Integer> levelmap = new HashMap<>();
levelmap.put(obj.getString("name"), 1);
while (!deque.isEmpty()){
JSONObject u = deque.poll();
try {
JSONArray children = u.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
deque.add(children.getJSONObject(i));
levelmap.put(children.getJSONObject(i).getString("name"), levelmap.get(u.getString("name")) + 1);
res.put(children.getJSONObject(i).getString("name"), u.getString("name") + "-" + levelmap.get(children.getJSONObject(i).getString("name")));
}
}catch (JSONException jex){
System.out.println("end of the tree");
}
}
//turn it back into a json array format
String str = new String("[]");
JSONArray jsonArray = new JSONArray(str);
System.out.println(res);
for(String key: res.keySet()){
String st = new String("{}");
JSONObject jsonObject = new JSONObject(st);
//key is parent
String[] tok = res.get(key).split("-");
String child = tok[0];
String mylevel = tok[1];
jsonObject.put("name", key);
jsonObject.put("level", mylevel);
jsonObject.put("parent", child);
jsonArray.put(jsonObject);
}
System.out.println(jsonArray.toString(2));
}
}
output:
[
{
"parent": "Hei Hei",
"level": "4",
"name": "Xiang Xiang"
},
{
"parent": "Lao Lao",
"level": "2",
"name": "Bao Miao"
},
{
"parent": "Lao Lao",
"level": "1",
"name": "NULL"
},
{
"parent": "Su Miao",
"level": "3",
"name": "Hei Hei"
},
{
"parent": "Hei Hei",
"level": "4",
"name": "Dan Dan"
},
{
"parent": "Hei Hei",
"level": "4",
"name": "Zai Zai"
},
{
"parent": "Li Xin",
"level": "4",
"name": "Xuan Xuan"
},
{
"parent": "Su Miao",
"level": "3",
"name": "Pang Pang"
},
{
"parent": "Li Xin",
"level": "4",
"name": "Fei Fei"
},
{
"parent": "Li Xin",
"level": "4",
"name": "To To"
},
{
"parent": "Bao Miao",
"level": "3",
"name": "Li Jing"
},
{
"parent": "Lao Lao",
"level": "2",
"name": "Su Miao"
},
{
"parent": "Bao Miao",
"level": "3",
"name": "Li Xin"
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With