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

相关推荐
皮皮林55133 分钟前
用 SpringBoot + Tess4j 优雅实现图像文字识别,真香!
spring boot
于先生吖3 小时前
Java+SpringBoot 无人健身房物联网系统完整源码实现
java·spring boot·物联网
johnrui3 小时前
SpringBoot-JdbcTemplate
java·spring boot·后端
indexsunny3 小时前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答解析
java·spring boot·redis·微服务·消息队列·电商
希望永不加班4 小时前
SpringBoot 过滤器(Filter)与请求链路梳理
java·spring boot·后端·spring
jiankeljx5 小时前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
Zzxy5 小时前
Spring Boot 参数校验
java·spring boot
云姜.6 小时前
JSON Schema使用
python·json
Zzxy7 小时前
Spring Boot集成Swagger/Knife4j
spring boot·swagger
用户8307196840829 小时前
Spring Boot 中Servlet、Filter、Listener 四种注册方式全解析
java·spring boot