java bean转Map
/**
* @author xxxxxxxxxxx
* @param object
* 待转化类
* @param format自定义转化类型
* @return Map<String,String> 将传入的DTO转化为Map,1、Date转化为String类型的时间戳。 2、null转化为null
* 3、enum通过反射使用getCode()获取code编码。4、其他类型不做转换value=toString()。5、Dto中属性名中的大写字母将被替换为
* "_小写字母"形式,为了与库中返回map一致。
*/
public static Map<String, String> objectToMap(Object object, FormatField format) {
if (object == null) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
Field[] declaredFields = object.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
try {
String key = field.getName();
if (key.equals("serialVersionUID")) {
continue;
}
String tmpKey=key;
for(int i=0;i<key.length();i++) {
if(Character.isUpperCase(key.charAt(i))) {
tmpKey=tmpKey.replaceAll(""+key.charAt(i), "_"+Character.toLowerCase(key.charAt(i)));
}
}
key=tmpKey;
Object value = field.get(object);
if (format != null) {
value = format.format(key, field.get(object));
}
if (value == null) {
map.put(key, null);
} else if (value instanceof Date) {
map.put(key, String.valueOf(((Date) value).getTime()));
} else if (value.getClass().isEnum()) {
Method method = value.getClass().getMethod("getCode", null);
Object val = method.invoke(value, null);
map.put(key, String.valueOf(val));
} else if (value.getClass().isPrimitive()) {
map.put(key, value + "");
} else {
map.put(key, value.toString());
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return map;
} public static interface FormatField {
public Object format(String key, Object value);
}
转载:https://www.cnblogs.com/dreammyle/p/5610906.html
/**
* 使用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();
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;
}
}
java bean转Map的更多相关文章
- Java bean 转 Map
Java bean 转 Map 时需要使用Fastjson //方法 一 Map<String, Object> a = (Map<String, Object>)JSON.t ...
- Java Bean与Map之间相互转化的实现
目录树 概述 Apache BeanUtils将Bean转Map Apache BeanUtils将Map转Bean 理解BeanUtils将Bean转Map的实现之手写Bean转Map 概述 Apa ...
- Bean与Map的转换 和 Map与Bean的转换
package com.JUtils.beanConvert; import java.beans.BeanInfo; import java.beans.IntrospectionException ...
- java bean、List、数组、map和Json的相互转化
工程 json包为 代码 package com.my.json; public class ChildBean { private String childName; private String ...
- java的bean和map互转的工具类
import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;i ...
- java中 json和bean list map之间的互相转换总结
JSON 与 对象 .集合 之间的转换 JSON字符串和java对象的互转[json-lib] 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级 ...
- Java中将JSON格式的数据转换成对应的Bean、Map、List数据
简单说明: 为了方便数据在客户端及服务器端的传输,有时候我们会用一些比较方便组织的数据类型,比如json.xml等传给客户端,客户端也可以重新组织数据传回服务器端.JSON和XML提供了一套比较方便的 ...
- bean 与 map 互转.
package com.sprucetec.tms.distribute.utils;import java.beans.BeanInfo;import java.beans.Introspectio ...
- 深入了解数据校验:Java Bean Validation 2.0(JSR380)
每篇一句 吾皇一日不退役,尔等都是臣子 相关阅读 [小家Java]深入了解数据校验(Bean Validation):基础类打点(ValidationProvider.ConstraintDescri ...
随机推荐
- oralce 创建表空间 和 查询进程
-- Create the user create user lesdba identified by les_321 default tablespace USERS temporary table ...
- css 字体单位之间的区分以及字体响应式实现
问题场景: 在实现响应式布局的过程中,如何设置字体大小在不同的视窗尺寸以及不同的移动设备的可读性? 需要了解的有: 1.px,em,pt之间的换算关系 1em = 16px 1px = 1/16 e ...
- 浅谈 extern "C"
今天上课实在无聊,就看了看 extern "C" 的作用,看了以后对它有了一点点理解,在这里给大家分享一下(本菜鸡水平有限,如若有说得不对的地方,还望大家指出). extern 关 ...
- idea和eclipse互相导入方法
1.eclipse maven项目导入idea 只需要在pom.xml文件中加入如下图 为了加载项目里的一些配置文件(例如.xml和.properties文件) 2.idea maven 项目导入ec ...
- uva1584 Circular Sequence(Uva-1584)
vj:https://vjudge.net/problem/UVA-1584 这个题讲的是一个圆环,圆环上面有一堆字母,找出字典序最小的那一圈 这个题我觉得直接用c语言的strcmp那一套感觉真是用不 ...
- JAVA中对事物的理解
1.事物是一组操作数据的集合动作 (场景:再开发的时候相信大家都遇到主表和子表的插入问题,当主表插入成功时,子表没有插入成功,这时候我们就要把主表的数据回滚,这个时候我们就要用到事物了) 2.一组处理 ...
- 第二节:numpy之数组切片、数据类型转换、随机数组
- 第十节:pandas之loc()、iloc()与ix()索引
- 【模板】最小生成树Kruskal
洛谷3366 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; ,ans=; struct e ...
- hdu 5176 The Experience of Love
The Experience of Love Accepts: 11 Submissions: 108 Time Limit: 4000/2000 MS (Java/Others) Memor ...