JAVA-JSON配置文件读取工具类

有些功能需要再配置文件里通过JSON形式进行配置。下面是一个JSON文件解析工具,方便解析JSON内容。

public class FileFormatUtil {
    protected static final Logger LOGGER = LoggerFactory.getLogger(FileFormatUtil.class);

    private static final String PATH = "classpath:conf/file-format.json";
    private static volatile Map<String, JSONObject> fileFormatMap = null;

    static {
        try {
            fileFormatMap = new HashMap<>();
            File file = ResourceUtils.getFile(PATH);
            String jsonString = FileUtils.readFileToString(file, "UTF-8");

            JSONArray jsonArray = new JSONArray(jsonString);
            jsonArray.stream().forEach( env -> {
                JSONObject jsonObject = (JSONObject) env;
                fileFormatMap.put(String.valueOf(jsonObject.get("id")), jsonObject);
            });
        }catch (Exception e) {
            LOGGER.error("file-format.json失败,", e);
        }
    }

    /***
     * 获取正则表达式
     * @param id
     * @return
     * @throws IOException
     */
    public static String getRegularExpression(String id) {
        JSONObject jsonObject = fileFormatMap.get(String.valueOf(id));
        return jsonObject == null? null : jsonObject.getStr("regularExpression");
    }


    public static Map<String, JSONObject> getAllFormat() {
        return fileFormatMap;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值