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

相关推荐
默 语2 天前
基于 Spring Boot 3 + LangChain4j 快速构建企业级 AI 应用实战
人工智能·spring boot·后端
薪火铺子2 天前
SpringBoot WebServer启动与监听器原理深度解析
spring boot·后端·tomcat
KmSH8umpK2 天前
SpringBoot 分布式锁实战:从单机锁到Redis分布式锁全覆盖,解决超卖、重复下单、幂等并发问题
spring boot·redis·分布式
jay神2 天前
基于团队模式的C程序设计课程辅助教学管理系统
java·spring boot·vue·web开发·管理系统
长河2 天前
基于 Jib 实现无 Dockerfile 的 Spring Boot 应用容器化
java·spring boot·后端
Arya_aa2 天前
一:病虫害 AI 识别系统项目初期准备与Docker初识,VM虚拟机
spring boot
敖正炀2 天前
Spring MVC 启动全景:DispatcherServlet 与父子容器
spring boot
绿草在线2 天前
基于SpringBoot4+Mybatis+Thymeleaf的用户管理系统开发实战
java·spring boot·thymeleaf
麦麦大数据2 天前
基于以太坊区块链+Spring Boot+Solidity智能合约的投票系统设计与实现
spring boot·后端·区块链·智能合约·投票系统