baidu_36967644 2021-09-12 23:21 采纳率: 0%
浏览 25

如何使用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的属性

  • 写回答

1条回答 默认 最新

  • weixin_40798006 2021-09-13 09:17
    关注
    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

    评论

报告相同问题?

问题事件

  • 修改了问题 9月12日
  • 创建了问题 9月12日

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计