Bean与Map的转换 和 Map与Bean的转换
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的转换的更多相关文章
- JSON,Bean,XML,List,Map
http://blog.csdn.net/superit401/article/details/51728929 JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把J ...
- java bean、List、数组、map和Json的相互转化
工程 json包为 代码 package com.my.json; public class ChildBean { private String childName; private String ...
- Dom4j把xml转换成Map(固定格式)
/** * 可解析list * * @param fileName * @return * @throws Exception */ @SuppressWarnings("unchecked ...
- Dom4j把xml转换成Map(非固定格式)
将xml转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.转换规则:1.主要是Map与List的互相嵌套2.同名称的节点会被装进List 示例: import java.util. ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
单词转换就是:将一些缩写的单词转换为实际的文本.第一个文件保存的是转换的规则,而第二个文件保存的是要转换的文本. 假设单词转换的规则的文件如下: brb be right back k okay? y ...
- Properties 转换成Map
转自:http://feitianbenyue.iteye.com/blog/1759259 对于Properties 转换成Map 的问题: 第一时间想到的肯定有以下: 1. 迭代出来 再 pu ...
- 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map
原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...
- 将Object对象转换成Map 属性名和值的形式
将Java对象转换成Map的键值对形式 代码: package cn.lonelcoud.util; import com.sun.deploy.util.StringUtils; import ja ...
随机推荐
- vue3作业
""" 1.按照上方 知识点总结 模块,总结今天所学知识点: 2.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/t ...
- 【JZOJ2156】【2017.7.10普及】复仇者vsX战警之训练
题目 月球上反凤凰装甲在凤凰之力附身霍普之前,将凤凰之力打成五份,分别附身在X战警五大战力上面辐射眼.白皇后.钢力士.秘客和纳摩上(好尴尬,汗). 在凤凰五使徒的至高的力量的威胁下,复仇者被迫逃到昆仑 ...
- maven 三个仓库表
https://search.maven.org ,http://www.mvnrepository.com/ http://maven.apache.org
- eclipse在线安装ermaster插件
eclipse在线安装ermaster插件: https://www.jianshu.com/p/449fbcd9141a ERMaster的安装和使用 https://www.cnblogs.com ...
- luogu 4059 [Code+#1]找爸爸 动态规划
Description 小A最近一直在找自己的爸爸,用什么办法呢,就是DNA比对.小A有一套自己的DNA序列比较方法,其最终目标是最 大化两个DNA序列的相似程度,具体步骤如下:1.给出两个DNA序列 ...
- BZOJ 4597: [Shoi2016]随机序列 线段树 + 思维
Description 你的面前有N个数排成一行.分别为A1, A2, … , An.你打算在每相邻的两个 Ai和 Ai+1 间都插入一个加号或者 减号或者乘号.那么一共有 3^(n-1) 种可能的表 ...
- 删除操作——str.subString(0,str.length()-1)
subString是String的一个方法,格式为: public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个 ...
- TCP连接的11种状态,三次握手四次挥手原因
1).LISTEN:首先服务端需要打开一个socket进行监听,状态为LISTEN. /* The socket is listening for incoming connections. 侦听来自 ...
- Harmonic Number (II)
Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I ...
- Spotlight_on_linux 安装和监控
一.下载 下载并安装 Spotlight_on_linux 二.建立连接 注意:用户名不能使用root连接,需要自己创建个用户root权限的用户 1.useradd xiaoxitest 2.p ...