获取项目中的后缀josn文档,转成JSON格式

工作中,用到了地图,有个地图的json文件,通过前端调用文件中的json数据。把文件放到项目中,ruoyi框架放到ruoyi-system下,resources下面,不然打包后找不到文件位置。

然后定义个url,指定这个位置

复制代码
private String filePath = "classpath:cjson/cj.json";

private JSONObject getJsonObject(String filePath) throws IOException{
    // 创建资源对象
    Resource resource = new ClassPathResource(filePath);
    // 获取资源文件的输入流
    InputStream inputStream = resource.getInputStream();
    // 读取输入流中的数据
    byte[] data = FileCopyUtils.copyToByteArray(inputStream);
    // 将字节数组转换为字符串
    String content = new String(data, StandardCharsets.UTF_8);
    // 输出文件内容
    return JSONObject.parse(content);
}

这样就可以把json后缀的文件里的内容,读取出来了,可以做自己业务。

相关推荐
梁云亮9 分钟前
Spring Boot + Thymeleaf 防重复提交
spring boot·防抖·防重复提交
sc写算法19 分钟前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
XMYX-07 小时前
Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)
spring boot·后端·prometheus
@yanyu6668 小时前
springboot实现查询学生
java·spring boot·后端
酷爱码9 小时前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
java干货10 小时前
虚拟线程与消息队列:Spring Boot 3.5 中异步架构的演进与选择
spring boot·后端·架构
武昌库里写JAVA13 小时前
iview Switch Tabs TabPane 使用提示Maximum call stack size exceeded堆栈溢出
java·开发语言·spring boot·学习·课程设计
小白杨树树13 小时前
【WebSocket】SpringBoot项目中使用WebSocket
spring boot·websocket·网络协议
不惑_14 小时前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json