讀取項目中json文件


public class SupplierInterfaceJsonUtil {

    /**
     * 獲取接口的json,轉換為字符串
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 獲取供應商接口數組
     * @return
     */
    public static JSONArray getSupplierInterfaces() {
        //獲取json文件路徑,在項目resource目錄下
        String path = SupplierInterfaceJsonUtil.class.getClassLoader().getResource("supplierInterface.json").getPath();
        //將json轉換為字符串
        String s = readJsonFile(path);
        //轉換為json數組返回
        if(s != null){
            JSONObject jsonObject = JSON.parseObject(s);
            String outInterface = jsonObject.getString("outInterface");
            if(outInterface != null && !outInterface.equals("")){
                JSONArray jsonArray = JSON.parseArray(outInterface);
                return jsonArray;
            }

        }
        return null;
    }

}

  xx


免責聲明!

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



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