这篇文章主要介绍了java实体类转成map的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
java实体类转成map
1.第一种
<!-- 配置gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Map<String, Object> map = null;
String jsonString = JSONUtil.toJsonStr(uploadBaseEntity);
Gson gson = new Gson();
map = new HashMap<>();
map = gson.fromJson(jsonString, map.getClass());
2.第二种
/**
* 对象转化为Map
*
* @param obj
* @return
* @throws Exception
*/
public static Map<String, String> objectToMap01(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), (String) field.get(obj));
}
return map;
}
java实体类与map集合互转
废话不说,直接上代码!留着用吧...
package com.ddm.message.test;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java实体类与Map互转
* @author Administrator
*
*/
public class entityMapTransUtils {
private static final Logger logger = LoggerFactory.getLogger(entityMapTransUtils.class);
/**
* Java实体类转Map:方法一
* @param obj
* @return
*/
public static Map<String, Object> entityToMap1(Object obj){
Map<String, Object> map = new HashMap<String, Object>();
Class<?> clazz = obj.getClass();
for(Field field : clazz.getDeclaredFields()){
field.setAccessible(true);
String fieldName = field.getName();
Object object = null;
try {
object = field.get(obj);
} catch (IllegalArgumentException | IllegalAccessException e) {
logger.info(e.getMessage());
}
map.put(fieldName, object);
}
return map;
}
/**
* Java实体类转Map:方法二
* @param obj
* @return
*/
public static Map<String, Object> entityToMap2(Object obj){
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
logger.info(e.getMessage());
}
return map;
}
/**
* Map转实体类:要转换的Map的key跟实体类属性名相同的数据会转过去,不相同的字段会为null
* @param clazz
* @param map
* @return
*/
public static <T> T mapToEntity1(Class<T> clazz,Map<String, Object> map){
T obj = null;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
obj = clazz.newInstance(); // 创建 JavaBean 对象
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
// 给 JavaBean 对象的属性赋值
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
try {
descriptor.getWriteMethod().invoke(obj, args);
} catch (InvocationTargetException e) {
logger.info(e.getMessage());
}
}
}
} catch (IllegalAccessException e) {
logger.info(e.getMessage());
} catch (IntrospectionException e) {
logger.info(e.getMessage());
} catch (IllegalArgumentException e) {
logger.info(e.getMessage());
} catch (InstantiationException e) {
logger.info(e.getMessage());
}
return (T)obj;
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:java实体类转成map的实现
基础教程推荐
猜你喜欢
- Java并发编程进阶之线程控制篇 2023-03-07
- Java文件管理操作的知识点整理 2023-05-19
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- springboot自定义starter方法及注解实例 2023-03-31
- java实现多人聊天系统 2023-05-19
- Java实现线程插队的示例代码 2022-09-03
- Java实现查找文件和替换文件内容 2023-04-06
- Java数据结构之对象比较详解 2023-03-07
- java基础知识之FileInputStream流的使用 2023-08-11