import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Map; /** * @program: postloan * @description: map转object * @author: Mr.xiaoshusheng * @create: 2019-04-08 14:38 **/ public class MapToObject { public static Object mapToObject(Map<Object, Object> map, Class<?> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) { continue; } field.setAccessible(true); if (map.containsKey(field.getName())) { field.set(obj, map.get(field.getName())); } } return obj; } }
map转对象(map转object)
最新推荐文章于 2024-07-28 17:31:28 发布