JavaBean 和 Map 之间互相转换
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; /**
* JavaBean and map converter.
*
*
*/
public final class BeanMapUtils { /**
* Converts a map to a JavaBean.
*
* @param type type to convert
* @param map map to convert
* @return JavaBean converted
* @throws IntrospectionException failed to get class fields
* @throws IllegalAccessException failed to instant JavaBean
* @throws InstantiationException failed to instant JavaBean
* @throws InvocationTargetException failed to call setters
*/
public static final Object toBean(Class<?> type, Map<String, ? extends Object> map)
throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
Object obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
}
}
return obj;
} /**
* Converts a JavaBean to a map.
*
* @param bean JavaBean to convert
* @return map converted
* @throws IntrospectionException failed to get class fields
* @throws IllegalAccessException failed to instant JavaBean
* @throws InvocationTargetException failed to call setters
*/
public static final Map<String, Object> toMap(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Map<String, Object> returnMap = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
}
JavaBean 和 Map 之间互相转换的更多相关文章
- 用反射实现JavaBean和Map之间的转换
		学习内容: 需求 由于JavaBean结构与Map类似,我们可以把JavaBean与Map进行转换 代码如下: package com.yy; import java.beans.BeanInfo; ... 
- 用jackson包实现json、对象、Map之间的转换
		jackson API的使用 用jackson包实现json.对象.Map之间的转换 
- 【java】之3种方式实现Object和Map之间的转换
		利用commons.BeanUtils实现Obj和Map之间转换,这种是最简单,也是最经常用的 public static Object mapToObject(Map<String, Obje ... 
- JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类
		<pre name="code" class="java"></pre><pre name="code" cl ... 
- Json,String,Map之间的转换
		前提是String的格式是map或json类型的 String 转Json JSONObject jasonObject = JSONObject.fromObject(str); String 转 ... 
- List和Map之间的转换和关联
		首先,Map.values返回的是此Map中包含的所有值的collection视图. 然后利用ArrayList的构造器ArrayList(Collection<? extends E> ... 
- guava处理字符串与List之间,字符串与map之间的转换<转>
		import static org.junit.Assert.*; import java.util.List; import java.util.Map; import org.junit.Test ... 
- JavaBean和json数据之间的转换(二)含有date类型的JavaBean
		1.前言 上次讲了简单的JavaBean和json格式之间的转换,代码很简单,但是实际过程中,往往用到的JavaBean都是比较复杂的,其他的字段还好,如果JavaBean中包含了date类型的字段, ... 
- JavaBean和json数据之间的转换(一)简单的JavaBean转换
		1.为什么要使用json? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,因为其高性能.可读性强的原因,成为了现阶段web开发中前后端交互数据的主要数据 ... 
随机推荐
- Mac环境下svn的使用(转载)
			在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ... 
- Servlet & JSP - 中文字符问题
			Servlet 中的中文字符 来自 URL 参数部分的中文字符 Tomcat 默认接收数据的编码是 ISO-8859-1.所以当请求 URL 的参数部分含有中文字符,需要转换字符的编码. Enumer ... 
- 我眼中的go的语法特点
			因为基本从c#/javascript/c/c++/python/lua/action script,一路走来,对所有的C系列的语法既熟悉又有好感: 那在看语言的时候肯定会与C系列的东西进行类比,那就总 ... 
- OC3-父类指针指向子类对象
			// // Cat.h // OC3-父类指针指向子类对象 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. ... 
- String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别
			以前刚入行的时候判断字符串的时候用 string a="a"; a==""; a==null; 后来发现了String.IsNullOrEmpty感觉方便了好多 ... 
- Transaction Script模式
			Transcation Script模式适合于小项目,维护量小的项目. 好比cs文件中有一个主方法,调用了本文件中的其他方法,如果说不需要怎么维护的话Tranacation Script模式就可以了, ... 
- 查看mysql集群状态是否正常
			如何查看mysql集群状态是否正常: 进入mysql 输入show status like 'wsrep%': 查看cluster sizes 是否为3 
- 百度云管家-V4.6.1-单文件版绿色版
			转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/baidu-is-clouds-butler-v4-6-1-single-file-green-edition ... 
- Poj 3117 World Cup
			1.Link: http://poj.org/problem?id=3117 2.Content: World Cup Time Limit: 1000MS Memory Limit: 65536 ... 
- 印象笔记无法同步问题解决 Unable to send HTTP request: 12029
			问题 今天突然发现本地软件不能访问网络. 包括: 印象笔记无法同步, 搜狗输入法无法登陆. 但其它上网正常. 思路及解决过程 因为chrome上网 ,qq上网均正常. 且同事可以正常使用. 推测是本地 ... 
