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

相关推荐
Momentary_SixthSense4 小时前
serde
开发语言·rust·json
MediaTea4 小时前
Python 文件操作:JSON 格式
开发语言·windows·python·json
Mos_x5 小时前
【Spring Boot】Spring Boot解决循环依赖
java·spring boot·spring
亚林瓜子5 小时前
AWS Elastic Beanstalk中安装tesseract5.3.4版本
spring boot·ocr·tesseract·aws·beanstalk·tess4j·eb
檐下翻书1737 小时前
Spring Boot 深度剖析:从虚拟线程到声明式 HTTP 客户端,再到云原生最优解
spring boot·http·云原生
计算机学姐8 小时前
基于SpringBoot的公务员考试管理系统【题库组卷+考试练习】
java·vue.js·spring boot·后端·java-ee·intellij-idea·mybatis
ejinxian11 小时前
protobuf 、JSON、XML传输格式
xml·json·protobuf
L.EscaRC13 小时前
Spring Security的解析与应用
spring boot·spring
小雨的光18 小时前
QuickEsView
spring boot·elasticsearch·es可视化
韩立学长18 小时前
基于Springboot的旧物公益捐赠管理系统3726v22v(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端