Avaneesh Kumar Avaneesh Kumar - 1 month ago 20
Java Question

How to get level of each object in json where objects can have any number of children of same object type

I have an Employee level structure like below imageenter image description here

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.

Answer Source

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 }