map和object互相转换
/**
* 使用org.apache.commons.beanutils进行转换
*/
class A { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map); return obj;
} public static Map<?, ?> objectToMap(Object obj) {
if(obj == null)
return null; return new org.apache.commons.beanutils.BeanMap(obj);
} } /**
* 使用Introspector进行转换
*/
class B { 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();
//默认PropertyDescriptor会有一个class对象,剔除之
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
}
return map;
}
}
/**
* 使用reflect进行转换
*/
class C {
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;
}
}
推荐使用方法二,方法一的内部实现也是方法二,区别是方法一不会剔除“class”这个key,obj转成map后,会多一个key为class,value为类路径的Entry
map和object互相转换的更多相关文章
- List<Map<String, Object>> 与 json 互转
近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件. 思路是 1.定时查询指纹,存到txt缓存文件中. 2.新增或删除指纹时,查询指纹,存到txt缓存文 ...
- 将Object对象转换成Map 属性名和值的形式
将Java对象转换成Map的键值对形式 代码: package cn.lonelcoud.util; import com.sun.deploy.util.StringUtils; import ja ...
- Java中Map和Object的互相转换方式
一.使用Apache中的BeanUtils类,导入commons-beanutils包. Jar包Maven下载地址:https://mvnrepository.com/artifact/common ...
- java实现map和object互转的三种方法
/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObje ...
- 8. Object转Map,Map转Object
法一:使用reflect进行转换 public static Object mapToObject(Map<String, Object> map, Class<?> bean ...
- JavaBean与Map<String,Object>相互转换
一.为什么要实现javaBean与Map<String,Object>相互转换 Spring中的BaseCommandController对象可以将传递过来的参数封装到一个JavaBean ...
- java中 json和bean list map之间的互相转换总结
JSON 与 对象 .集合 之间的转换 JSON字符串和java对象的互转[json-lib] 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级 ...
- Array、Set、Map、Object学习总结
Array和Set对比 都是一个存储多值的容器,两者可以互相转换,但是在使用场景上有区别.如下: Array的indexOf方法比Set的has方法效率低下 Set不含有重复值(可以利用这个特性实现对 ...
- Bean与Map的转换 和 Map与Bean的转换
package com.JUtils.beanConvert; import java.beans.BeanInfo; import java.beans.IntrospectionException ...
随机推荐
- jQuery使用正则判断是否含有非法字符
if(/[@#\$%\^&\*]+/gi.test($("#user_api_register_form").find("input[name='user_nam ...
- 架构-LAMP特级学习(网站加速解决方案)
1.Squid代理缓存技术 2.页面静态化缓存技术 3.Memcache.Redis等缓存服务器 4.Sphinx搜索加速
- MySQL JOIN操作报错问题小解
1 问题描述 在调用一个MySQL存储过程的时候,有时候会出现下面的错误: Illigal mix of collations(gbk\_chinese\_ci, IMPLICIT) and (lat ...
- eclipse 垃圾回收器,内存释放
http://zhangrong-0825-163-com.iteye.com/blog/7334071.如何在eclipse里修改web工程的访问路径,步骤如下: 点击web工程——>选择pr ...
- Python学习笔记010——匿名函数lambda
1 语法 my_lambda = lambda arg1, arg2 : arg1 + arg2 + 1 arg1.arg2:参数 arg1 + arg2 + 1 :表达式 2 描述 匿名函数不需要r ...
- SYS_R12 MOAC多组织的四个应用(案例)
2014-05-31 Created By BaoXinjian
- Linux下C结构体初始化
1.前言 今天在公司看一同事写的代码,代码中用到了struct,初始化一个struct用的是乱序格式,如下代码所示: typedef struct _data_t { int a; int b; }d ...
- codeforces#254DIV2解题报告
今天简直大爆发啊... 吃了顿烧烤竟然这么管事. . . .. 本弱渣竟然做出来了3道,并且B题是我第一次在CF中用到算法..(曾经最多也就是贪心. . . ). 题目地址:codeforces#22 ...
- jenkins 执行远程linux命令
在Jenkins中进行构建时,可能需要首先SSH登录到一个远程服务器以执行必要的脚本,然后再执行构建.这时,需要安装SSH Plugin,并进行如下配置.1.在Jenkins界面,系统管理->管 ...
- unity 获得子节点
transform.FindChild("childName") transform.FindChild("childName/grandChildName") ...