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

相关推荐
han_hanker30 分钟前
@Validated @Valid 用法
java·spring boot
言慢行善43 分钟前
SpringBoot中的注解介绍
java·spring boot·后端
许杰小刀2 小时前
MyBatis-Plus实战:Spring Boot数据库操作效率提升10倍
数据库·spring boot·mybatis
霸道流氓气质3 小时前
SpringBoot中使用SpringAIAlibaba框架集成阿里云百炼实现AI快速对话入门示例
人工智能·spring boot·阿里云
认真的小羽❅3 小时前
从入门到精通:Spring Boot 整合 MyBatis 全攻略
spring boot·后端·mybatis
捧月华如4 小时前
响应式设计原理与实践:适配多端设备的前端秘籍
前端·前端框架·json
珍朱(珠)奶茶5 小时前
Spring Boot3整合Jxls工具包实现模版excel导出文件
spring boot·后端·excel
不懂的浪漫6 小时前
mqtt-plus 架构解析(一):分层架构与设计哲学
spring boot·分布式·物联网·mqtt·架构
海兰6 小时前
【springboot】gradle快速镜像配置
spring boot·笔记·后端
武超杰7 小时前
SpringBoot 整合 Spring Security 实现权限控制
spring boot·后端·spring