获取项目中的后缀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后缀的文件里的内容,读取出来了,可以做自己业务。

相关推荐
2601_949817721 天前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
uNke DEPH1 天前
Spring Boot的项目结构
java·spring boot·后端
zhenxin01221 天前
Spring Boot 3.x 系列【3】Spring Initializr快速创建Spring Boot项目
spring boot·后端·spring
zhenxin01221 天前
【wiki知识库】07.用户管理后端SpringBoot部分
spring boot·后端·状态模式
SeSs IZED1 天前
SSM与Springboot是什么关系? -----区别与联系
java·spring boot·后端
勿忘,瞬间1 天前
SpringBoot配置文件
java·spring boot·后端
2601_949814491 天前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
wellc1 天前
SpringBoot集成Flowable
java·spring boot·后端
tongxh4231 天前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
hERS EOUS1 天前
Spring Boot + Spring AI快速体验
人工智能·spring boot·spring