package com.JUtils.beanConvert;

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; /**
* Bean与Map的转换
*
* @since 1.0.0
*/
public class BeanMapConvert {
/**
* Bean转换为Map
*
* @param
* @return String-Object的HashMap
* @since v1.0.0
*/ public static void main(String[] args) { //Bean转换为Map
Berth b = new Berth("45", 55d, 65d, 6565d, 63d, 9666l, 9656l, "fgj");
Map<String, Object> stringObjectMap = bean2MapObject(b);
System.out.println("Bean转换为Map "+stringObjectMap); //Map转换为Bean
Map<String,Object> map = new HashMap<String,Object>();
map.put("berthId","berthId");
map.put("coastBearing",6556d);
map.put("faceBearing",5645d);
map.put("lat",6896868d);
map.put("lon",6868868d);
map.put("ppdAta",525l);
map.put("sldAta",66666l);
map.put("type","fdgfd");
Berth nn = new Berth();
Object o = map2Bean(map, nn);
System.out.println("Map转换为Bean "+o); }
public static Map<String,Object> bean2MapObject(Object object){
if(object == null){
return null;
} Map<String, Object> map = new HashMap<String, Object>();
try { //Introspector类:
//  将JavaBean中的属性封装起来进行操作。在程序把一个类当做JavaBean来看,
// 就是调用Introspector.getBeanInfo()方法,得到的BeanInfo对象封装了把这个类当做JavaBean的结果信息,即属性的信息。 BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(object); map.put(key, value);
}
}
} catch (Exception e) {
e.printStackTrace();
} return map;
} /**
* Map转换为Java Bean
*
* @param map
* 待转换的Map
* @param object
* Java Bean
* @return java.lang.Object
*/
public static Object map2Bean(Map map,Object object){
if(map == null || object == null){
return null;
}
try {
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(object, value);
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return object;
}
}

Bean与Map的转换 和 Map与Bean的转换的更多相关文章

  1. JSON,Bean,XML,List,Map

    http://blog.csdn.net/superit401/article/details/51728929 JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把J ...

  2. java bean、List、数组、map和Json的相互转化

    工程 json包为  代码 package com.my.json; public class ChildBean { private String childName; private String ...

  3. Dom4j把xml转换成Map(固定格式)

    /** * 可解析list * * @param fileName * @return * @throws Exception */ @SuppressWarnings("unchecked ...

  4. Dom4j把xml转换成Map(非固定格式)

    将xml转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.转换规则:1.主要是Map与List的互相嵌套2.同名称的节点会被装进List 示例: import java.util. ...

  5. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map

    单词转换就是:将一些缩写的单词转换为实际的文本.第一个文件保存的是转换的规则,而第二个文件保存的是要转换的文本. 假设单词转换的规则的文件如下: brb be right back k okay? y ...

  7. Properties 转换成Map

    转自:http://feitianbenyue.iteye.com/blog/1759259 对于Properties 转换成Map 的问题: 第一时间想到的肯定有以下: 1.  迭代出来  再 pu ...

  8. 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map

    原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...

  9. 将Object对象转换成Map 属性名和值的形式

    将Java对象转换成Map的键值对形式 代码: package cn.lonelcoud.util; import com.sun.deploy.util.StringUtils; import ja ...

随机推荐

  1. Java基本的程序结构设计 控制流程

    控制流程 java的控制流程和C和C++基本一致,只是不能使用goto语句,不过break语句可以带标签,实现从内层循环跳出的目的.标签可以放在for或者while前面.如下: package com ...

  2. 负载均衡(四)Nginx负载均衡策略

    一.Nginx的作用 1.反向代理 代理:转发请求的服务器,分代理和反向代理.代理一般指的是我们使用的DNS,反向代理是放在服务端的大家通常用Nginx来解决.实际应用中,由于服务端处于一个中心位置, ...

  3. uwsgi 的启动、停止、重启

    ## 一.概念释义### WSGI WSGI 是一个Web服务器(如nginx)与应用服务器(如uWSGI)通信的一种规范(协议).官方定义是,the Python Web Server Gatewa ...

  4. ES6---new Promise()讲解(尤其注意里面的参数resolve、reject)

    直接打印出来看看吧,console.dir(Promise). 这么一看就明白了,Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.cat ...

  5. [洛谷P3322] SDOI2015 排序

    问题描述 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的 i(1<=i<=N),第i中操作为将序列 ...

  6. 使用tensorflow训练word2vec

    from http://blog.csdn.net/wangyangzhizhou/article/details/77530479?locationNum=1&fps=1 使用了tensor ...

  7. Javascript高级程序设计第三版-笔记

    1.JS数值最大值最小值: >Number.MIN_VALUE <5e-324 >Number.MAX_VALUE <1.7976931348623157e+308 判断数值是 ...

  8. 微信小程序登录 code 40029 天坑

    微信登录时 code 大坑(服务端返回如下代码) {"errcode":40029,"errmsg":"invalid code, hints: [ ...

  9. Spring Cloud云架构 - SSO单点登录之OAuth2.0登录流程(2)

    上一篇是站在巨人的肩膀上去研究OAuth2.0,也是为了快速帮助大家认识OAuth2.0,闲话少说,我根据框架中OAuth2.0的使用总结,画了一个简单的流程图(根据用户名+密码实现OAuth2.0的 ...

  10. python之新式类与经典类

    经典类与新式类 经典类:P 或 P()--深度查找,向上查父节点 新式类 :P(object)---广度查找,继承object,新式类的方法较多