import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Cleanup;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.io.InputStream;
public class JsonUtils {
/**
* 读取json配置文件
*
* @param jsonPath json文件路径
* @return JSONObject格式数据
*/
public static JSONObject getJSONObject (String jsonPath) {
Resource resource = new ClassPathResource(jsonPath);
JSONObject jsonObject = new JSONObject();
try {
if (resource.exists()) {
// 自动关闭流
@Cleanup
InputStream inputStream = resource.getInputStream();
Object object = JSON.parseObject(inputStream, JSONObject.class);
if (object instanceof JSONObject) {
jsonObject = (JSONObject) object;
}
}
} catch (IOException e) {
return jsonObject;
}
return jsonObject;
}
}
Json工具类
最新推荐文章于 2025-06-25 14:06:51 发布
1355

被折叠的 条评论
为什么被折叠?



