基於fastjson的工具類

之前工作boss要做遠程調用,叫我先用json文件模擬後端數據庫進行操作,然後就寫了一大堆的對json的處理方法來模擬數據庫輸的CRUD操作,記錄一下,以備以後再用。

package com.jst.spbootmvcdemo.web.testUitl;
/*
 * @author  Salong
 * @date  2019/9/5 11:26
 * @Email:[email protected]
 * 基於fastjson的工具類
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.*;

public class jsonUtil {
    //判斷json最外層是對象還是數組
    public static String objectOrArray(String jsonString){
        char goal=jsonString.trim().charAt(0);
        if('['==goal){
            return "array";
        }else if ('{'==goal){
            return "object";
        }
        return "bad json!";
    }

    //json文件變字符串
    public static String JsonFileToString(String AbsolutePathFile) throws IOException {
        // 讀取json文件數據
        StringBuffer strbuffer = new StringBuffer();
        File myFile = new File(AbsolutePathFile);
        if (!myFile.exists()) {
             System.err.println("未找到文件: " + AbsolutePathFile);
        }
        try {
            FileInputStream fis = new FileInputStream(AbsolutePathFile);
            InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
            BufferedReader in = new BufferedReader(inputStreamReader);
            String str;
            while ((str = in.readLine()) != null) {
                strbuffer.append(str);
            }
            in.close();
        } catch (IOException e) {
            e.getStackTrace();
        }
        return strbuffer.toString();
    }

    //將json寫入文件中
    public static void jsonWriteInFile(String jsonStr,String filePath){
        FileOutputStream fos=null;
            try {
                fos = new FileOutputStream(filePath);
                fos.write(jsonStr.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

    }

    //通過json中的某屬性值刪除所有包含這個屬性值的最小單位對象
    public static void deleteObjByKeyValue(JSONObject jsonObject,String key,Object objValue){
        String Str=jsonObject.toJSONString();
        String numGoal="\""+key+"\":"+objValue.toString();
        String strGoal="\""+key+"\":\""+objValue.toString()+"\"";
        if(!Str.contains(numGoal)&&!Str.contains(strGoal)){
            System.out.println("未能從"+Str+"中找到key是"+key+",value是"+objValue+"的對象!");
        } else if(Str.contains(numGoal)){
            //進行刪除對象操作
            deleteObjByKeyValue(Str,numGoal);
        }
        if (Str.contains(strGoal)){
            //進行刪除對象操作
            deleteObjByKeyValue(Str,strGoal);
        }
    }

    //內部業務服務代碼,不提供對外使用
    private static String deleteObjByKeyValue(String parentStr,String goal){
        String[] arrStr=parentStr.split(goal);
        if(arrStr.length>2){
            return "發現多個符合目標key,value值的對象,尚不提供刪除操作!";
        }
        String returnJsonStr=null;
        // TODO: 2019/9/6 多個目標的屬性值的對象刪除操作
        //只有一個的目標屬性的切割
        for(int i=0;i<2;i++){
            String todoStr=arrStr[i];
            if(i%2==0){
                todoStr=todoStr.substring(0,todoStr.lastIndexOf("{"));
            }else{
                int index=todoStr.indexOf("}");
                char c=todoStr.charAt(index+1);
                if(','==c){
                    todoStr=todoStr.substring(index+2);
                }else{
                    todoStr=todoStr.substring(index+1);
                    arrStr[i-1]=arrStr[i-1].substring(0,arrStr[i-1].length()-1);
                }
            }
            arrStr[i]=todoStr;
            returnJsonStr=arrStr[0]+arrStr[1];
        }
        return returnJsonStr;
    }

    //判斷是否存在某節點
    public static boolean jsonKeyExits(JSONObject obj,String key) {
        String jsonStr=obj.toString();
        String goalKey="\""+key+"\":";
        if(jsonStr.contains(goalKey)){
            return true;
        }
        return false;
    }

    //判斷是否存在key和value都相同的節點
    public static boolean jsonKeyExits(JSONObject obj,String key, String value) {
        String str=obj.toString();
        System.out.println(str);
        String goalJson="\""+key+"\":"+value;
        String goal="\""+key+"\":\""+value+"\"";
        if(str.contains(goalJson)||str.contains(goal)){
            return true;
        }
        return false;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章