Java -读写文件( json) - 无键值,转换JSONArray数组 | 有键值 转换为JSONObject | JSONArray 转换为 List<JSONObject> 集合

注意

当我们在Java中使用FileInputStream来读取文件时,需要注意关闭InputStream的问题。FileInputStream是一种资源,它需要占用系统资源来进行读文件操作。如果在使用完FileInputStream后不进行关闭操作,就会导致系统资源被占用且无法释放,最终影响程序的性能与稳定性。

因此,在使用完FileInputStream后,我们应该调用其close()方法来释放已占用的系统资源,以防止因此导致的各种问题,例如内存泄漏、程序异常终止等。

读取完数据后,就必须关闭,不然不仅浪费资源,也容易导致新建文件后,该文件内容会重复之前的数据(笔者遇到过的奇怪问题,类似缓存问题)。

一、读取文件内容

1、无键值JSONArray,转换 json 数组、JSONArray 转换为 List 集合

测试数据格式:

[
  {
      "owner_ip": 0,
      "id": 0,
      "text": "test",
      "timestamp": ""
  },
  {
      "owner_ip": 0,
      "id": 1,
      "text": "test",
      "timestamp": ""
  },
  {
      "owner_ip": 0,
      "id": 2,
      "text": "test",
      "timestamp": ""
  },
  {
      "owner_ip": 0,
      "id": 3,
      "text": "test",
      "timestamp": ""
  },
  {
      "owner_ip": 0,
      "id": 4,
      "text": "test",
      "timestamp": ""
  }
]

具体 Java 代码演示:

import java.io.File;
import java.io.FileInputStream;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;


String filePath = "/test/file.json";
FileInputStream testfis = null;
testfis = new FileInputStream(filePath);
JSONArray jsonArray = JSON.parseObject(testfis, JSONArray.class);
testfis.close(); //关闭InputStream
System.err.println(jsonArray);


for(int i=0;i<jsonArray.size();i++){
		System.err.println(jsonArray.get(i));
        JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
        System.err.println(jsonObject.get("id"));
        System.err.println(jsonObject.get("text"));
}

// 转换为 List<JSONObject> 集合

List<JSONObject> testData = new ArrayList<>();
testData = JSON.parseArray(jsonArray.toJSONString(), JSONObject.class);

for(int i=0;i<testData.size();i++){
    System.err.println(testData.get(i).get("owner_ip"));
	System.err.println(testData.get(i).get("id"));
	System.err.println(testData.get(i).get("text"));
	System.err.println(testData.get(i).get("timestamp"));
}

2、有键值 转换为 JSONObject、JSONArray 转换为 List 集合

{ 	
	"data": "这是测试数据!"
	"listData":[
	{
      "owner_ip": 0,
      "id": 0,
      "text": "test",
      "timestamp": ""
	},
  	{
      "owner_ip": 0,
      "id": 1,
      "text": "test",
      "timestamp": ""
 	 }]
}

具体 Java 代码演示:

import java.io.File;
import java.io.FileInputStream;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;


String filePath = "/test/file.json";
FileInputStream testfis = null;
testfis = new FileInputStream(filePath);
JSONObject jsonObject = JSON.parseObject(new FileInputStream(filePath), JSONObject.class);
testfis.close(); //关闭InputStream
System.err.println(jsonObject );

String data = (String) jsonObject.get("data");
JSONArray listData = jsonObject.getJSONArray("listData");

for(int i=0;i<listData.size();i++){
		System.err.println(listData.get(i));
        JSONObject current= JSONObject.parseObject(listData.get(i).toString());
        System.err.println(current.get("id"));
        System.err.println(current.get("text"));
}

// 转换为 List<JSONObject> 集合

List<JSONObject> testData = new ArrayList<>();
testData = JSON.parseArray(listData.toJSONString(), JSONObject.class);

for(int i=0;i<testData.size();i++){
    System.err.println(testData.get(i).get("owner_ip"));
	System.err.println(testData.get(i).get("id"));
	System.err.println(testData.get(i).get("text"));
	System.err.println(testData.get(i).get("timestamp"));
}

二、写入文件

JSONObject jsonObj = new JSONObject();
//向jsonObj中添加数据:{"adapter":"WLAN","ip_address":"192.168.1.6"}
jsonObj.put("ip_address", "192.168.1.6");
jsonObj.put("adapter", "WLAN");
System.out.println("要添加到JSON文件中的数据:"+jsonObj);
//写入操作
BufferedWriter bw = null;
try {
       bw = new BufferedWriter(new FileWriter("D:\\ipAddressConfig.json"));
       bw.write(jsonObj.toString());//转化成字符串再写
       bw.close();
   } catch (IOException ex) {
       throw new RuntimeException(ex);
   }

参考链接

JSON文件的读写(java)

用Java读取JSON文件

解析Json数组java

java json发送 java实现json文件的读取和解析 -文件流读取
-hutool工具读取

Java中JSON数据的读取和解析

文件解析__JSON解析

Java中的FileInputStream是否需要close问题

详解json转list:从对象到集合

JSON数组形式字符串转换为List的8种方法

如何使用Java获取Json中的数据

用Java解析JSON多维数组

关于“ java实现json文件的读取和解析 ” 的推荐:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值