Java把Map轉(zhuǎn)為對(duì)象的實(shí)現(xiàn)代碼
1、map轉(zhuǎn)java對(duì)象
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.mes.common.core.utils.StringUtils; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger;
/**
* @Author: best_liu
* @Description: map轉(zhuǎn)java對(duì)象
* @Date: 8:35 2022/3/22
**/
public static Object convertToObject(Class clazz, Map<String, Object> map) throws
IntrospectionException, InstantiationException, IllegalAccessException {
BeanInfo bi = Introspector.getBeanInfo(clazz);
Object obj = clazz.newInstance();
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
String pName;
for (PropertyDescriptor pd : pds) {
pName = pd.getName();
if (map.containsKey(pName)) {
try {
if (pd.getPropertyType().getName().equals("java.lang.Long")) {
if(StringUtils.isNotEmpty(map.get(pName).toString())){
pd.getWriteMethod().invoke(obj, Long.parseLong(map.get(pName).toString()));
}
// else{
// pd.getWriteMethod().invoke(obj, map.get(pName));
// }
} else {
pd.getWriteMethod().invoke(obj, map.get(pName));
}
} catch (Exception ex) {
Logger.getLogger(MapUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return obj;
}2、java對(duì)象轉(zhuǎn)map
/**
* @Author: best_liu
* @Description: java對(duì)象轉(zhuǎn)map
* @Date: 8:35 2022/3/22
**/
public static Map<String, String> objectToMap(Object obj) {
if (obj == null) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
String v = null;
if (value != null) {
v = value.toString();
}
map.put(key, v);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* @Author: best_liu
* @Description: java對(duì)象轉(zhuǎn)map
* @Date: 8:35 2022/3/22
**/
public static Map<String, Object> objectToMapObj(Object obj) {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}3、Map轉(zhuǎn)換為Json字符串
/**
* @Author: best_liu
* @Description: Map轉(zhuǎn)換為Json字符串
* @Date: 8:35 2022/3/22
**/
public static String mapToJsonString(Map map) {
JSONObject json = new JSONObject();
Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
json.put(entry.getKey(), entry.getValue());
}
return json.toString();
}4、Json字符串轉(zhuǎn)換為Map
/**
* @Author: best_liu
* @Description: Json字符串轉(zhuǎn)換為Map
* @Date: 8:35 2022/3/22
**/
public static Map<String, Long> jsonStringToMap(String str) {
Map<String, Long> map = (Map<String, Long>) JSON.parse(str);
return map;
}到此這篇關(guān)于Java把Map轉(zhuǎn)為對(duì)象的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Java Map轉(zhuǎn)為對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud如何使用Feign構(gòu)造多參數(shù)的請(qǐng)求
這篇文章主要介紹了Spring Cloud如何使用Feign構(gòu)造多參數(shù)的請(qǐng)求,以GET以及POST方法的請(qǐng)求為例進(jìn)行講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知
本篇文章主要介紹了詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
java并發(fā)編程JUC CountDownLatch線程同步
這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來展開對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容2021-09-09
構(gòu)建Java樹結(jié)構(gòu)的三種實(shí)現(xiàn)方法
這篇文章主要介紹了構(gòu)建Java樹結(jié)構(gòu)的三種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
Spring通過攔截器實(shí)現(xiàn)多數(shù)據(jù)源切換的示例代碼
本文主要介紹了Spring攔截器實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享
這篇文章主要介紹了java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享,本文直接給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03

