亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java把Map轉(zhuǎn)為對象的實現(xiàn)代碼

 更新時間:2023年08月29日 10:55:33   作者:Best_Liu~  
在項目開發(fā)中,經(jīng)常碰到map轉(zhuǎn)實體對象或者對象轉(zhuǎn)map的場景,工作中,很多時候我們可能比較喜歡使用第三方j(luò)ar包的API對他們進行轉(zhuǎn)化,但這里,我想通過反射的方式對他們做轉(zhuǎn)化,感興趣的同學跟著小編來看看吧

1、map轉(zhuǎn)java對象

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對象
     * @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對象轉(zhuǎn)map

/**
     * @Author: best_liu
     * @Description: java對象轉(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對象轉(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)為對象的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Java Map轉(zhuǎn)為對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java SpringBoot自定義注解,及自定義解析器實現(xiàn)對象自動注入操作

    java SpringBoot自定義注解,及自定義解析器實現(xiàn)對象自動注入操作

    這篇文章主要介紹了java SpringBoot自定義注解,及自定義解析器實現(xiàn)對象自動注入操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • SpringBoot如何實現(xiàn)持久化登錄狀態(tài)獲取

    SpringBoot如何實現(xiàn)持久化登錄狀態(tài)獲取

    這篇文章主要介紹了SpringBoot 如何實現(xiàn)持久化登錄狀態(tài)獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java動態(tài)代理語法Proxy類原理詳解

    Java動態(tài)代理語法Proxy類原理詳解

    這篇文章主要介紹了Java動態(tài)代理語法Proxy類原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java中Session的詳解

    Java中Session的詳解

    這篇文章主要介紹了了解java中的session的相關(guān)問題,什么是session,session怎么用等,具有一定參考價值,需要的朋友可以了解下。
    2021-10-10
  • PowerJob的WorkerHealthReporter工作流程源碼解讀

    PowerJob的WorkerHealthReporter工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的WorkerHealthReporter工作流程源碼解讀,
    2023-12-12
  • 詳解spring注解配置啟動過程

    詳解spring注解配置啟動過程

    這篇文章主要為大家詳細介紹了詳解spring注解配置啟動過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 基于Freemarker和xml實現(xiàn)Java導(dǎo)出word

    基于Freemarker和xml實現(xiàn)Java導(dǎo)出word

    這篇文章主要介紹了基于Freemarker和xml實現(xiàn)Java導(dǎo)出word,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • 詳解Java中的OkHttp?JSONP爬蟲

    詳解Java中的OkHttp?JSONP爬蟲

    一般在java平臺上,我們會使用apache?httpclient作為http客戶端,用于發(fā)送?http?請求,并對響應(yīng)進行處理,這篇文章主要介紹了詳解Java中的OkHttp?JSONP爬蟲的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Java常用面板之JScrollPane滾動面板實例詳解

    Java常用面板之JScrollPane滾動面板實例詳解

    這篇文章主要介紹了Java常用面板JScrollPane的簡單介紹和一個相關(guān)實例,,需要的朋友可以參考下。
    2017-08-08
  • 淺析SpringBoot中的過濾器和攔截器

    淺析SpringBoot中的過濾器和攔截器

    過濾器和攔截器都是為了在請求到達目標處理器(Servlet或Controller)之前或者之后插入自定義的處理邏輯,下面就跟隨小編來看看它們二者的區(qū)別和具體使用吧
    2024-03-03

最新評論