map转化为对象
来源:互联网 发布:首选网络类型哪个好 编辑:程序博客网 时间:2024/06/04 18:44
如何把Map 转化为指定对象呢?
先看测试:
@Test public void test_convertMap2Obj() throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{ Map map=new HashMap(); map.put("personName", "黄威"); map.put("birthdate", new Timestamp(1000255)); map.put("identitify", "422203158895255555"); map.put("age", 21); Object obj=ReflectHWUtils.convertMap2Obj(map, Person2.class); Person2 person=(Person2)obj;// System.out.println(obj); System.out.println(person.getPersonName()); System.out.println(person.getAge()); System.out.println(person.getBirthdate()); }
运行结果:
黄威
21
1970-01-01 08:16:40.255
ReflectHWUtils.convertMap2Obj实现如下:
/*** * convert map to object ,see setObjectValue(obj, map) * @param map : key是对象的成员变量,其value就是成员变量的值 * @param clazz * @return * @throws InstantiationException * @throws IllegalAccessException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException */ public static Object convertMap2Obj(Map<String, Object> map,Class clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{ if(ValueWidget.isNullOrEmpty(map)){ return null; } Object obj=clazz.newInstance(); setObjectValue(obj, map); /*for(Iterator it=map.entrySet().iterator();it.hasNext();){ Map.Entry<String, Object> entry=(Map.Entry<String, Object>)it.next(); String key=entry.getKey(); Object val=entry.getValue(); }*/ return obj; }/*** * 利用反射设置对象的属性值. 注意:属性可以没有setter 方法. * * @param obj * @param params * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void setObjectValue(Object obj, Map<String, Object> params) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { if(ValueWidget.isNullOrEmpty(obj)){ return; } if (ValueWidget.isNullOrEmpty(params)) { return; } Class<?> clazz = obj.getClass(); for (Iterator it = params.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it .next(); String key = entry.getKey(); Object propertyValue = entry.getValue(); if (ValueWidget.isNullOrEmpty(propertyValue)) { continue; } Field name = getSpecifiedField(clazz, key); if (name != null) { name.setAccessible(true); name.set(obj, propertyValue); } } }
依赖的方法:
/*** * 利用反射设置对象的属性值. 注意:属性可以没有setter 方法. * * @param obj * @param params * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void setObjectValue(Object obj, Map<String, Object> params) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { if(ValueWidget.isNullOrEmpty(obj)){ return; } if (ValueWidget.isNullOrEmpty(params)) { return; } Class<?> clazz = obj.getClass(); for (Iterator it = params.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it .next(); String key = entry.getKey(); Object propertyValue = entry.getValue(); if (ValueWidget.isNullOrEmpty(propertyValue)) { continue; } Field name = getSpecifiedField(clazz, key); if (name != null) { name.setAccessible(true); name.set(obj, propertyValue); } } } /*** * Get Specified Field * * @param clazz * @param fieldName * @return */ public static Field getSpecifiedField(Class<?> clazz, String fieldName) { Field f = null; if (ValueWidget.isNullOrEmpty(clazz)||ValueWidget.isNullOrEmpty(fieldName)) { return null; } try { f = clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { Class superClass2=clazz.getSuperclass(); if (superClass2.getName().equals(Object.class.getName())){ return null; } return getSpecifiedField(superClass2/* * may be null if it is * Object . */, fieldName); // e.printStackTrace(); } return f; }
源代码见:
源代码下载地址
跨浏览器实现placeholder
0 0
- map转化为对象
- Java 对象转化为Map
- java对象转化为Map
- Map对象通过反射转化为JavaBean对象转化
- 将json字符串转化为Map对象
- Map 对象转化为一个 JavaBean
- java实现对象转换为map、xml字符串转化为map、map转化为xml字符串
- java中将jsonObject字符串转化为Map对象
- JAVA解析JSON数据转化为Map对象
- 将一个 JavaBean 对象转化为一个 Map
- 将一个 JavaBean 对象转化为一个 Map
- java中将jsonObject字符串转化为Map对象
- List,Map,Bean等对象转化为Json的方式
- Array转化为Map
- Json 转化为Map
- bean转化为map
- map转化为list
- JSON转化为Map
- 350个LINUX命令详解(英文完整版)
- Spring MVC 框架搭建及详解
- 手斧Linux – 从LFS到Funtoo (71)
- 工具类,为上传文件命名
- 手斧Linux – 从LFS到Funtoo (72)
- map转化为对象
- 终于毕业了
- 3、算法
- linux命令英文缩写的含义
- 栈溢出笔记(1)
- Linux 监控每秒网卡流量
- 手斧Linux – 从LFS到Funtoo (73)
- spring声明式事务
- Implementing & Consuming ASP.NET WEB API from JQuery (MVC 4)