如何使用java代码将树状结构里所有childrenList为空的数据的所有父节点都罗列出来

加入JSON数据如下:

[
    {
        "id": 1,
        "name": "1",
        "parentId": 0,
        "childrenList": [
            {
                "id": 2,
                "name": "2",
                "parentId": 1,
                "childrenList": [
                    {
                        "id": 6,
                        "name": "6",
                        "parentId": 2,
                        "childrenList": []
                    }
                ]
            },
            {
                "id": 5,
                "name": "5",
                "parentId": 1,
                "childrenList": []
            }
        ]
    },
    {
        "id": 3,
        "name": "3",
        "parentId": 0,
        "childrenList": [
            {
                "id": 4,
                "name": "4",
                "parentId": 3,
                "childrenList": []
            }
        ]
    }
]

输出:

List<Object> objectList1 的集合里面包含了 id为1和id为2以及id为6的属性
List<Object> objectList2 的集合里面包含了 id为1和id为5的属性
List<Object> objectList3 的集合里面包含了 id为3和id为4的属性

import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.util.function.Consumer;

public class JsonUtils {

    public static void main(String args[]) throws JSONException {
        String jsonstr = "[\n" +
                "  {\n" +
                "    \"id\": 1,\n" +
                "    \"name\": \"1\",\n" +
                "    \"parentId\": 0,\n" +
                "    \"childrenList\": [\n" +
                "      {\n" +
                "        \"id\": 2,\n" +
                "        \"name\": \"2\",\n" +
                "        \"parentId\": 1,\n" +
                "        \"childrenList\": [\n" +
                "          {\n" +
                "            \"id\": 6,\n" +
                "            \"name\": \"6\",\n" +
                "            \"parentId\": 2,\n" +
                "            \"childrenList\": []\n" +
                "          }\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": 5,\n" +
                "        \"name\": \"5\",\n" +
                "        \"parentId\": 1,\n" +
                "        \"childrenList\": []\n" +
                "      }\n" +
                "    ]\n" +
                "  },\n" +
                "  {\n" +
                "    \"id\": 3,\n" +
                "    \"name\": \"3\",\n" +
                "    \"parentId\": 0,\n" +
                "    \"childrenList\": [\n" +
                "      {\n" +
                "        \"id\": 4,\n" +
                "        \"name\": \"4\",\n" +
                "        \"parentId\": 3,\n" +
                "        \"childrenList\": []\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "]";
        JSONArray jsonArray = new JSONArray(jsonstr);
        consumerArr(jsonArray);
    }

    /**
     * 分析JSON
     * @param jb
     */
    public static void consumerJson(JSONObject jb) {
            try {
                JSONArray childrenList = jb.getJSONArray("childrenList");
                if (childrenList.length() == 0) {
                    System.out.println("id=" + jb.get("id"));
                }else{
                    consumerArr(childrenList);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    /**
     * 分析json 数组
     * @param ja
     * @throws JSONException
     */
    public static void consumerArr(JSONArray ja) throws JSONException {
            for (int i = 0;i < ja.length(); i++ ) {
                JSONObject jsonObject = ja.getJSONObject(i);
                consumerJson(jsonObject);
            }
        }
}


输出结果
id=6
id=5
id=4