java 对象转map

本文介绍了一种将JavaBean对象转换为Map的方法,并提供了一个示例类`BeanToMapUtil`,它能够递归地处理JavaBean及其包含的集合类型的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BeanToMapUtil {


/**
* @param args
* @throws InvocationTargetException 
* @throws IllegalAccessException 
* @throws IntrospectionException 
*/
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
// TODO Auto-generated method stub


userd d=new userd(12,"ccc",88,"plt",new String[]{"panlitao","panwenxuan","dengxiaofen"});

Map mp=convertBean(d);

System.out.println(((List<Map>)mp.get("fjlist")).get(1).get("uname").toString()); 

System.out.println(((List<Map>)mp.get("fjlist")).get(1).get("uname").toString()); 
}





/**
* 将一个 JavaBean 对象转化为一个 Map

* @param bean
*            要转化的JavaBean 对象
* @return 转化出来的 Map 对象
* @throws IntrospectionException
*             如果分析类属性失败
* @throws IllegalAccessException
*             如果实例化 JavaBean 失败
* @throws InvocationTargetException
*             如果调用属性的 setter 方法失败
*/
public static Map convertBean(Object bean) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);


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) {
if(readMethod.toString().indexOf("java.util.List")>-1&&result.toString().indexOf("@")>-1)
{
  List<Object> lmp=new ArrayList<Object>();
for(Object mobj:((List<Object>)result))
{lmp.add(convertBean(mobj));}
returnMap.put(propertyName, lmp);
}
else if(result.toString().indexOf("@")>-1&&result.toString().indexOf("[Ljava.lang.")==-1)
{returnMap.put(propertyName, convertBean(result));}
else
{returnMap.put(propertyName, result);}
}

else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
}


class userd  {

userd(int uid,String uname,int age,String up,String[] arg)
{
this.uid=uid;
this.uname=uname;
this.age=age;
this.up=up;
this.arg=arg;
}
public String[] getArg() {
return arg;
}
public void setArg(String[] arg) {
this.arg = arg;
}
public fjben getFn() {
return fn;
}
public void setFn(fjben fn) {
this.fn = fn;
}

private List<String> strlist;


public List<String> getStrlist() {

List<String> lst=new ArrayList<String>();
lst.add("aaa");
lst.add("bbb");
strlist=lst;
return strlist;
}
public void setStrlist(List<String> strlist) {
this.strlist = strlist;
}


private List<fjben> fjlist;
public List<fjben> getFjlist() {
List<fjben> fl=new ArrayList<fjben>();
fl.add(new fjben(99,"ccc99",9999,"plt99",new String[]{"99999","7777","55555"}));
fl.add(new fjben(991,"ccc991",99991,"plt991",new String[]{"999991","77771","555551"}));
fjlist=fl;
return fjlist;
}
public void setFjlist(List<fjben> fjlist) {
this.fjlist = fjlist;
}


private fjben fn=new fjben(88,"ccc88",8888,"plt88",new String[]{"panlitao88","panwenxuan88","dengxiaofen88"});
private String[] arg;
private int uid;
private String uname;
private int age;
private String up;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUp() {
return up;
}
public void setUp(String up) {
this.up = up;
}

}




class fjben  {

fjben(int uid,String uname,int age,String up,String[] arg)
{
this.uid=uid;
this.uname=uname;
this.age=age;
this.up=up;
this.arg=arg;
}
public String[] getArg() {
return arg;
}
public void setArg(String[] arg) {
this.arg = arg;
}
private String[] arg;
private int uid;
private String uname;
private int age;
private String up;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUp() {
return up;
}
public void setUp(String up) {
this.up = up;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值