Java-根据路径获取JSON字符串的value值

发布于:2025-05-23 ⋅ 阅读:(17) ⋅ 点赞:(0)

实现内容

入参: 一串json的字符串,根据传入的字符串路径和想要获取此路径下的key值

出参: 此路径下的key的value值

实现原理

采用一层一层获取的方式,判断第一层并且获取第一层的数据,放入到数组中,将该数组放到下一次循环,再获取下一层的数据

实现方法

    /**
     * 根据路径获取JSON的数据
     * @param entityString JSON的string字符串
     * @param routeCode 路径 ROOT|BODY|OUT_DATA|OWE_LIST
     * @param subjectParam 需要获取的value
     * @return 返回数组或者value值
     * @throws Exception 异常抛出
     */
    Object parsingJSON(String entityString,String routeCode, String subjectParam) throws Exception{
        Object obj = JSON.parse(entityString);
        String[] routeList = (routeCode + "|" + subjectParam).split("\\|");
        Object currentObject = obj;
        // 循环路径
        for (String route : routeList) {
            // 如果是数组-解析数组
            if (currentObject instanceof JSONArray) {
                // 用于汇聚子数据
                JSONArray c = new JSONArray();
                // 循环子数据
                for (int i = 0; i < ((JSONArray) currentObject).size(); i++) {
                    // 获取子数据
                    Object arrItem = ((JSONArray) currentObject).get(i);
                    // 检查子数据是否为数组
                    if (arrItem instanceof JSONArray) {
                        // 循环子数据的子数据
                        for (int j = 0; j < ((JSONArray) arrItem).size(); j++) {
                            Object arrItemItem = ((JSONArray) arrItem).get(j);
                            // 只处理到子数据的子数据----- 只需要关心两层数据,多层以上不关心
                            if (arrItemItem instanceof JSONObject) {
                                Object arrItemKey = ((JSONObject) arrItemItem).get(route);
                                if (null != arrItemKey) {
                                    c.add(arrItemKey);
                                }
                            }
                        }
                    } else {
                        Object arrItemKey = ((JSONObject) arrItem).get(route);
                        if (null != arrItemKey) {
                            c.add(arrItemKey);
                        }
                    }
                }
                currentObject = c;
                // 不是数组就直接替换
            } else {
                currentObject = ((JSONObject) currentObject).get(route);
            }
        }
        return currentObject;
    }

调用展示

public static void main(String[] args) {
        String entityString = "{\n" +
                "    \"ROOT\": {\n" +
                "        \"BODY\": {\n" +
                "            \"RETURN_MSG\": \"测试\",\n" +
                "            \"RETURN_CODE\": \"200\",\n" +
                "            \"USER_MSG\": \"实例\",\n" +
                "            \"DETAIL_MSG\": \"订购实例\",\n" +
                "            \"OUT_DATA\": {\n" +
                "                \"OWE_LIST\": [\n" +
                "                    {\n" +
                "                        \"CONNCT_TEL\": \" \",\n" +
                "                        \"CUST_ID\": \"11005000006099\",\n" +
                "                        \"OWE_AMOUNT\": [\n" +
                "                            {\n" +
                "                                \"TEXT\": [\n" +
                "                                    {\n" +
                "                                        \"a1\": 121231\n" +
                "                                    },\n" +
                "                                    {\n" +
                "                                        \"a1\": 121421\n" +
                "                                    }\n" +
                "                                ]\n" +
                "                            },\n" +
                "                            {\n" +
                "                                \"TEXT\": [\n" +
                "                                    {\n" +
                "                                        \"a1\": 121421\n" +
                "                                    },\n" +
                "                                    {\n" +
                "                                        \"a1\": 122121\n" +
                "                                    }\n" +
                "                                ]\n" +
                "                            }\n" +
                "                        ]\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"CONNCT_TEL\": \" \",\n" +
                "                        \"CUST_ID\": \"11005000006091\",\n" +
                "                        \"OWE_AMOUNT\": [\n" +
                "                            {\n" +
                "                                \"TEXT\": [\n" +
                "                                    {\n" +
                "                                        \"a1\": 121241\n" +
                "                                    },\n" +
                "                                    {\n" +
                "                                        \"a1\": 123121\n" +
                "                                    }\n" +
                "                                ]\n" +
                "                            },\n" +
                "                            {\n" +
                "                                \"TEXT\": [\n" +
                "                                    {\n" +
                "                                        \"a1\": 121221\n" +
                "                                    }\n" +
                "                                ]\n" +
                "                            }\n" +
                "                        ]\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            \"REQUEST_ID\": \"20270907183443371_1_78\",\n" +
                "            \"RUN_IP\": \"111.111.111.111\",\n" +
                "            \"PROMPT_MSG\": \"\"\n" +
                "        }\n" +
                "    }\n" +
                "}\n";

        String routeCode = "ROOT|BODY|OUT_DATA|OWE_LIST|OWE_AMOUNT|TEXT";
        String subjectParam = "a1";
        try {
            Object res = parsingJSON(entityString,routeCode,subjectParam);
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


网站公告

今日签到

点亮在社区的每一天
去签到