首页 > 编程知识 正文

将object转为map,map怎么转换成JAVAbeen

时间:2023-05-04 01:59:37 阅读:17259 作者:3565

//*

将JavaBean对象转换为Map

*

* @param bean转换的JavaBean对象

* @return转换的Map对象

* @throws IntrospectionException类属性分析失败时

* @ throwsillegalaccessexceptionjavabean实例化失败时

调用* @ throwsinvocationtargetexception属性的setter方法将失败

*/

公共统计转换bean (对象bean )。

throws IntrospectionException,IllegalAccessException,InvocationTargetException {

Class type=bean.getClass (;

Map returnMap=new HashMap (;

beaninfobeaninfo=introspect or.getbeaninfo (type );

property descriptor [ ] property descriptors=bean info.getproperty descriptors (;

for(intI=0; i propertyDescriptors.length; I ) {

propertydescriptordescriptor=property descriptors [ I ];

string property name=descriptor.getname (;

if (! propertyname.equals('class ' ) }{

methodreadmethod=descriptor.getreadmethod (;

对象结果=read method.invoke (bean,new Object[0];

if (结果!=空) {

Returnmap.put(propertyname,result );

} else {

Returnmap.put(propertyname,'');

}

}

}

返回返回返回映射;

}

译文: 3359 www.cn blogs.com/Lin sky/p/12015199.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。