法一:使用reflect进行转换

public static Object mapToObject(Map<String, 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);
field.set(obj, map.get(field.getName()));
}
return obj;
} public static Map<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null){
return null;
} Map<String, Object> map = new HashMap<String, Object>(); Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
} return map;
} 法二:使用Introspector进行转换
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null; Object obj = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
setter.invoke(obj, map.get(property.getName()));
}
} return obj;
} public static Map<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null)
return null; Map<String, Object> map = new HashMap<String, Object>(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
} return map;
}
 

8. Object转Map,Map转Object的更多相关文章

  1. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  2. 笔记 freemark list标签迭代Map<Map<String,Object>集合排序问题

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. 工作中出现一个比较特殊的问题,在模板ftl文件中,一般用list迭代map 举例: 后台: // 传入的参数 ...

  3. 用于把List<Object>转换成Map<String,Object>形式

    /** * 用于把List<Object>转换成Map<String,Object>形式,便于存入缓存 * @author zhang_bo * @param keyName ...

  4. list中依据map&lt;String,Object&gt;的某个值排序

    private void sort(List<Map<String, Object>> list) { Collections.sort(list, new Comparato ...

  5. 把List<Map<String,Object>>转成Map<String,Object>

    Map<String, Object> parmMap = new HashMap<String, Object>(); //定义一个用于存储强转后的Map List<M ...

  6. fastjson解析list ,object中含有list, object中含有map

    1.首先定义测试vo package com.haiyisoft.cAssistantWeb.ui; import java.sql.Timestamp; public class vo {priva ...

  7. java object bean 转map

    import java.lang.reflect.Field; /** * obj-->map * ConvertObjToMap * 2016年8月17日上午10:53:59 * @param ...

  8. ES6 Map vs ES5 Object

    ES6 Map vs ES5 Object Map vs Object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  9. PHP json_decode object时报错Cannot use object of type stdClass as array

    PHP json_decode object时报错Cannot use object of type stdClass as array php再调用json_decode从字符串对象生成json对象 ...

  10. [ES6] When should use Map instead of Object

    Use Maps when keys are unknown until runtime: Map: let recentPosts = new Map(); createPost( newPost, ...

随机推荐

  1. 多线程练习,深刻体会了一次变量的BUG.

    package ltb20180106; public class TestBankThread { private int deposit=0;//注意全局变量的摆放. public TestBan ...

  2. Tomacat 配置

    server.xml文件中元素: 1.<Service name="Catalina"> 这个元素相当于IIS的一个网站.该元素可有多个.每个元素会根据名字在conf文 ...

  3. elasticsearch 口水篇(1) 安装、插件

    一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...

  4. Survival Analysis

    code{white-space: pre;} Survival Analysis Zhu Lin 2017-3-18 What is Survival Analysis Survival analy ...

  5. 峰Redis学习(7)Redis 之Keys 通用操作

    keys * 显示所有key   查找所有以s开头的key 用s*  *代表任意字符 127.0.0.1:6379> keys s* 1) "set3" 2) "s ...

  6. hadoop长时间运行后,stop-all.sh报错

    报错现象: hadoop在stop-all.sh的时候依据的是datanode上的mapred和dfs进程号. 而默认的进程号保存在/tmp下,linux默认会每 隔一段时间(一般是一个月或者7天左右 ...

  7. 堆叠箱子(基础dp)

    P1086 时间限制: 1 Sec  内存限制: 128 MB提交: 38  解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 现有N种箱子,每种箱子高度H_i,数量C_i.现选取 ...

  8. 集群Redis使用 Python pipline大批量插入数据

    class myRedis(object):     def __init__(self,redis_type=None,**args):         if redis_type == " ...

  9. Ribbon Workbench The plug-in execution failed because the Sandbox Client encountered an error during initialization

    使用 Ribbon Workbench打开解决方案时,出现The plug-in execution failed because the Sandbox Client encountered an ...

  10. Windows Server 2012 R2 无法启用Microsoft .NET Framework 3.5 功能

    1 在新windows 2012 R2 上安装SQL 2014 ,提示需要安装 .NET Framework 3.5 2 在添加角色和功能--功能--.NET Framework 3.5,然后失败 3 ...