java 获取json的jsonPah(com.alibaba.fastjson)



//获取jsonPath 
    public static  List getListJsonPath(JSONObject jsonObject) {

        List jsonPaths= JSONPath.paths(jsonObject).keySet().stream().collect(Collectors.toList());
        List parentNode=new ArrayList<>();
        //根节点
        parentNode.add("/");
        for(int i=1;i0){

                parentNode.add(jsonPaths.get(i).substring(0,jsonPaths.get(i).lastIndexOf("/")));

            }
        }
        //删除父节点的key
        for(String p:parentNode){
            jsonPaths.remove(p);
        }
        List jsonPathList=new ArrayList<>();
        Iterator jsonpath=jsonPaths.iterator();
        //将/t替换成.
        while (jsonpath.hasNext()){
            String  tempjsonPath=jsonpath.next();
            for(int j=tempjsonPath.length();--j>=0;){

                if(Character.isDigit(tempjsonPath.charAt(j))){
                    //将0.1.2.3.4 替换为[1] [2] [3] [4]
                    tempjsonPath=  tempjsonPath.replaceAll("/"+ tempjsonPath.charAt(j),"["+tempjsonPath.charAt(j)+"]");
                }
            }
            jsonPathList.add(tempjsonPath.replaceFirst("/", "").replaceAll("/", "."));
        }
        return jsonPathList;
    }



获取json路径上的值(com.jayway.jsonpath)



//读取值
    public static String readjson(String json, String jsonPath) {
        try {
            Object value = JsonPath.read(json, jsonPath, new Predicate[0]);
            if (value instanceof Integer) {
                return value.toString();
            } else if (value instanceof String) {
                return value.toString();
            } else if (value instanceof Boolean) {
                return value.toString();
            } else if (value instanceof JSONArray) {
                JSONArray arr = (JSONArray) value;
                if (!arr.isEmpty()) {
                    return arr.toJSONString();
                } else
                    return "";
            } else if (value instanceof LinkedHashMap) {
                return value.toString();
            } else if (value instanceof Float) {
                return value.toString();
            } else {
                return value.toString();
            }
        } catch (Exception e) {
            return "path not found";
        }
    }



测试方法



@Test
    public void jsonReplaceVal(){
        //构造json
        JSONObject jsonObject = new JSONObject();
        JSONPath.set(jsonObject,"data.person","个人");
        JSONPath.set(jsonObject,"data.student[0].age","20");
        JSONPath.set(jsonObject,"data.student[0].name[0].v","张三");
        JSONPath.set(jsonObject,"data.student[1].age","20");
        JSONPath.set(jsonObject,"data.student[1].name[1].nn","张三");
        JSONPath.set(jsonObject,"data.student[1].name[0].nn","张");
        //获取路径
        List listjsonPath=getListJsonPath(jsonObject);
        System.out.println("listJsonPath:"+listjsonPath.toString());
        //将路径上的内容替换
        for(int i=0;i0) {
                String lastFields = fields[fields.length - 1];
                DocumentContext ext = JsonPath.parse(jsonObject);
                if (lastFields.equals("nn")) {
                    JsonPath p = JsonPath.compile("$." + tempPath);
                    //可先取值在对值进行处理
                    ext.set(p, "lhs");
                    String NewList = ext.jsonString();
                    System.out.println("NewsList:" + NewList);
                }
            }

        }
    }



至此可完成对json子节点的数据处理