Java读取json文件转换为json

时间:2021-05-24 04:37:23   收藏:0   阅读:0

在项目中遇到读取以json格式存储的配置文件,为了方便操作,将文件内容读取出来并转换为json对象,本文使用的是fastjson

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 读取配置文件
 * @author oldwei
 */
public class ConfJsonUtil {
    public static JSONObject toJson() throws IOException {
        String filePath = System.getProperty("user.dir") + "/sdk/conf.json";
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        String conf = IOUtils.toString(in, StandardCharsets.UTF_8);
        return JSON.parseObject(conf);
    }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!