JAVA 提取json路徑,並對路徑上的值進行脫敏或更改


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

    //獲取jsonPath 
    public static  List<String> getListJsonPath(JSONObject jsonObject) {

        List<String> jsonPaths= JSONPath.paths(jsonObject).keySet().stream().collect(Collectors.toList());
        List<String> parentNode=new ArrayList<>();
        //根節點
        parentNode.add("/");
        for(int i=1;i<jsonPaths.size();i++){
            if (jsonPaths.get(i).lastIndexOf("/")>0){

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

            }
        }
        //刪除父節點的key
        for(String p:parentNode){
            jsonPaths.remove(p);
        }
        List<String> jsonPathList=new ArrayList<>();
        Iterator<String> 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<String> listjsonPath=getListJsonPath(jsonObject);
        System.out.println("listJsonPath:"+listjsonPath.toString());
        //將路徑上的內容替換
        for(int i=0;i<listjsonPath.size();i++){
          String  tempPath=  listjsonPath.get(i);
          String[] fields=tempPath.split("\\.");
          if (fields.length>0) {
                String lastFields = fields[fields.length - 1];
                DocumentContext ext = JsonPath.parse(jsonObject);
                if (lastFields.equals("nn")) {
                    JsonPath p = JsonPath.compile("$." + tempPath);
//可先取值在對值進行處理 ext.set(p,
"呂厚帥"); String NewList = ext.jsonString(); System.out.println("NewsList:" + NewList); } } } }

至此可完成對json子節點的數據處理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM