package com.mhw.json.group.test;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @program: lomir-ai-box
* @description: Json分组示例
* @author: ext.manhengwei1
* @create: 2023-06-09 09:08
**/
public class JsonGroupingExample {
/**
* 主函数,读取Excel文件数据并进行处理
*
* @param args main函数参数
* @throws IOException 文件读取异常
*/
public static void main(String[] args) throws IOException {
// Excel文件路径
String fileName = "C:\\Users\\ext.manhengwei1\\Desktop\\格式一模版.xlsx";
// 将文件路径转换为Spring框架MultipartFile类型
MultipartFile file = filePath2MultipartFile(fileName);
// 读取Excel文件数据并将其转换为List<Map>形式
List<Map<String, String>> dataList = EasyExcel.read(file.getInputStream()).sheet().doReadSync();
// 去掉前两行数据
dataList.subList(0, 2).clear();
// 遍历数据列表,将Map中key的类型转换为String类型
for (Map<String, String> map : dataList) {
Map<String, String> newMap = new HashMap<>(map.size());
for (Map.Entry<String, String> entry : map.entrySet()) {
newMap.put(String.valueOf(entry.getKey()), entry.getValue());
}
map.clear();
map.putAll(newMap);
}
// 将List<Map>转换为JSON数组
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(dataList));
// 存放处理后的人员信息列表
List<JSONObject> personList = new ArrayList<>();
// 当前正在处理的人员信息
JSONObject currentPerson = null;
// 遍历JSON数组
for (int i = 0; i < jsonArray.size(); i++) {
// 获取当前对象
JSONObject jsonObject = jsonArray.getJSONObject(i);
// 如果当前对象中包含key为"0"的数据,则认为该对象是新的人员信息
if (jsonObject.containsKey("0")) {
// 如果当前正在处理的人员信息不为null,则将其添加到列表中
if (currentPerson != null) {
personList.add(currentPerson);
}
// 新建一个人员信息,并添加childData属性
currentPerson = jsonObject;
currentPerson.put("childData", new ArrayList<>());
// 获取childData属性列表
List<JSONObject> childData = currentPerson.getObject("childData", List.class);
// 存放需要删除的key列表
List<String> keysToRemove = new ArrayList<>();
// 新建一个JSON对象,用于存放子项数据
JSONObject child = new JSONObject();
// 遍历当前人员信息,将key不为"childData",且下标大于等于23的数据添加到child对象中
for (String key : currentPerson.keySet()) {
if (!"childData".equals(key) && Integer.parseInt(key) >= 23) {
if (!"实施".equals(currentPerson.getString(key)) && !"不实施".equals(currentPerson.getString(key))) {
keysToRemove.add(key);
child.put(key, currentPerson.getString(key));
// 将child对象添加到childData列表中
if (child.size() > 0) {
if (childData.size() > 0 && childData.get(childData.size() - 1).equals(child)) {
childData.get(childData.size() - 1).putAll(child);
} else {
childData.add(child);
}
}
}
}
}
// 删除key
keysToRemove.forEach(currentPerson::remove);
} else {
// 当前对象属于前一个人的数据,将其添加到前一个对象的列表属性中
if (currentPerson != null) {
List<JSONObject> childData = currentPerson.getObject("childData", List.class);
childData.add(jsonObject);
}
}
}
// 将最后一个人员信息添加到列表中
if (currentPerson != null) {
personList.add(currentPerson);
}
// 遍历人员信息列表,输出每个人员信息的JSON字符串
for (JSONObject person : personList) {
System.out.println(JSON.toJSONString(person));
}
}
private static MultipartFile filePath2MultipartFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream input = new FileInputStream(file);
return new MockMultipartFile(file.getName(),
file.getName(), "text/plain", input);
}
}
文章评论