JAVABEAN递归转MAP实现
之前想找这么一个方法,找到的都不是递归实现的,列表、MAP里面的都没转,就自己折腾了个。——YOYO
public class ObjectToMap{
public static Map objectToMap(Object obj){
try{
Class type = obj.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(obj, new Object[0]);
if(result == null){
continue;
}
//判断是否为 基础类型 String,Boolean,Byte,Short,Integer,Long,Float,Double
//判断是否集合类,COLLECTION,MAP
if(result instanceof String
|| result instanceof Boolean
|| result instanceof Byte
|| result instanceof Short
|| result instanceof Integer
|| result instanceof Long
|| result instanceof Float
|| result instanceof Double
|| result instanceof Enum
){
if (result != null) {
returnMap.put(propertyName, result);
}
}else if(result instanceof Collection){
Collection<?> lstObj = arrayToMap((Collection<?>)result);
returnMap.put(propertyName, lstObj);
}else if(result instanceof Map){
Map<Object,Object> lstObj = mapToMap((Map<Object,Object>)result);
returnMap.put(propertyName, lstObj);
} else {
Map mapResult = objectToMap(result);
returnMap.put(propertyName, mapResult);
}
}
}
return returnMap;
}catch(Exception e){
throw new RuntimeException(e);
}
}
private static Map<Object, Object> mapToMap(Map<Object, Object> orignMap) {
Map<Object,Object> resultMap = new HashMap<Object,Object>();
for(Entry<Object, Object> entry:orignMap.entrySet()){
Object key = entry.getKey();
Object resultKey = null;
if(key instanceof Collection){
resultKey = arrayToMap((Collection)key);
}else if(key instanceof Map){
resultKey = mapToMap((Map)key);
}
else{
if(key instanceof String
|| key instanceof Boolean
|| key instanceof Byte
|| key instanceof Short
|| key instanceof Integer
|| key instanceof Long
|| key instanceof Float
|| key instanceof Double
|| key instanceof Enum
){
if (key != null) {
resultKey = key;
}
}else{
resultKey = objectToMap(key);
}
}
Object value = entry.getValue();
Object resultValue = null;
if(value instanceof Collection){
resultValue = arrayToMap((Collection)value);
}else if(value instanceof Map){
resultValue = mapToMap((Map)value);
}
else{
if(value instanceof String
|| value instanceof Boolean
|| value instanceof Byte
|| value instanceof Short
|| value instanceof Integer
|| value instanceof Long
|| value instanceof Float
|| value instanceof Double
|| value instanceof Enum
){
if (value != null) {
resultValue = value;
}
}else{
resultValue = objectToMap(value);
}
}
resultMap.put(resultKey, resultValue);
}
return resultMap;
}
private static Collection arrayToMap(Collection lstObj){
ArrayList arrayList = new ArrayList();
for (Object t : lstObj) {
if(t instanceof Collection){
Collection result = arrayToMap((Collection)t);
arrayList.add(result);
}else if(t instanceof Map){
Map result = mapToMap((Map)t);
arrayList.add(result);
} else {
if(t instanceof String
|| t instanceof Boolean
|| t instanceof Byte
|| t instanceof Short
|| t instanceof Integer
|| t instanceof Long
|| t instanceof Float
|| t instanceof Double
|| t instanceof Enum
){
if (t != null) {
arrayList.add(t);
}
}else{
Object result = objectToMap(t);
arrayList.add(result);
}
}
}
return arrayList;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?