读取项目中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