当前位置: 首页 > news >正文

Java 生成和读取JSON文件

下面的demo当中 ,是将json文件放到了zip包当中。如果不需要,可以拿掉。

1、生成对象JSON文件

	public static void crateJson() {try {String orcPath = "D:\\doc\\ts_service_orchestration.json";// 对象集合或者对象都可以List<DataPO> dataPOList = new ArrayList<>();String jsonString = JSONObject.toJSONString(dataPOList);// 生成json文件tempFile(orcPath, jsonString);FileInputStream fileInputStream = null;int length;byte[] b = new byte[1024];int len;String path =  "D:\\doc\\压缩包.zip";File zipfile = new File(path);if (!zipfile.exists()) {zipfile.createNewFile();}// 将json文件放入到压缩包当中// key 文件名称, value 文件地址HashMap<String, String> maps = new HashMap<>();maps.put("ts_service_orchestration.json", orcPath);ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));for (Map.Entry<String, String> entry : maps.entrySet()) {File newFile = new File(entry.getValue());fileInputStream = new FileInputStream(newFile);out.putNextEntry(new ZipEntry(entry.getKey()));while ((len = fileInputStream.read(b)) > 0){out.write(b, 0, len);}out.closeEntry();fileInputStream.close();}out.close();// delete jsonFilenew File(orcPath).delete();}catch (Exception e){e.printStackTrace();}}public static void tempFile(String filePath, String jsonData) throws IOException {// 保证创建一个新文件File file = new File(filePath);if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录file.getParentFile().mkdirs();}if (file.exists()) { // 如果已存在,删除旧文件file.delete();}file.createNewFile();// 格式化json字符串jsonData = JsonUtil.formatJson(jsonData);// 将格式化后的字符串写入文件Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");write.write(jsonData);write.flush();write.close();}

2、读取json文件

	public static void readJson(){try {// 转为压缩文件流ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream("D:\\doc\\压缩包.zip"), Charset.forName("gbk"));ZipEntry zipEntry = null;while ((zipEntry = zipInputStream.getNextEntry()) != null) {if (!zipEntry.isDirectory() && zipEntry.getName().endsWith(".json")) {// Read the Excel file from the Zip entryByteArrayOutputStream outputStream = new ByteArrayOutputStream();byte[] buffer = new byte[4096];int length = -1;while ((length = zipInputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, length);}outputStream.close();tempReadFile(outputStream);zipInputStream.closeEntry();}}zipInputStream.close();} catch (IOException e) {e.printStackTrace();}}public static void tempReadFile(ByteArrayOutputStream outputStream) throws IOException {String jsonStr = "";Reader reader = new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray()),"utf-8");int ch = 0;StringBuffer sb = new StringBuffer();while ((ch = reader.read()) != -1) {sb.append((char) ch);}reader.close();jsonStr = sb.toString();// 这里注意,如果是json文件当中是对象集合的话可以这样写,但是如果是对象的话,这样转换是会出错的。JSONArray array = JSONObject.parseArray(jsonStr);for (Object o : array) {JSONObject jsonObject = (JSONObject)o;System.out.println(jsonObject);}System.out.println("=================================================================");}
http://www.lryc.cn/news/209856.html

相关文章:

  • k8s-----26、细粒度权限管理 RBAC
  • 【Unity ShaderGraph】| 制作一个 高级流体水球效果
  • 日常软件游戏丢失msvcp120dll怎么修复?分享5个修复方法
  • 自动驾驶之—2D到3D升维
  • ubuntu18.4(后改为20.4)部署chatglm2并进行基于 P-Tuning v2 的微调
  • 爬虫-获取数据xpath
  • SpringBoot中使用JdbcTemplate访问Oracle数据库
  • 【Linux】权限完结
  • 计算机网络-应用层(3)
  • 虎去兔来(C++)
  • docker基础镜像定制
  • 解决git action定时任务执行失败的方法
  • Node编写重置用户密码接口
  • Day13力扣打卡
  • 独立开发者知识贴
  • 软考系列(系统架构师)- 2009年系统架构师软考案例分析考点
  • C语言每日一题(21)删除排序数组中的重复项
  • 如何快速解决d3dcompiler_43.dll缺失问题?五种方法快速解决
  • mongodb数据迁移的方法
  • Spring MVC 中文文档
  • RedissonCach的源码流程
  • spring-基于注解管理bean
  • 数据挖掘(7.1)--数据仓库
  • Vue3问题:如何实现密码加密登录?前后端!
  • 【爬虫】python打包可执行程序(ui界面制作完成后)
  • 取Dataset子集(pytorch)
  • 如何选择消息队列
  • 读取mysql数据库表结构生成接口文档
  • 【MySQL索引与优化篇】InnoDB数据存储结构
  • Go学习第十二章——Go反射与TCP编程