最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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)求

    這篇文章主要介紹了Spring Cloud如何使用Feign構(gòu)造多參數(shù)的請(qǐng)求,以GET以及POST方法的請(qǐng)求為例進(jìn)行講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • SpringBoot使用minio及配置代碼

    SpringBoot使用minio及配置代碼

    MinIO是一個(gè)非常輕量的服務(wù),可以很簡(jiǎn)單的和其他應(yīng)用的結(jié)合,類似?NodeJS,?Redis?或者?MySQL。本文重點(diǎn)給大家介紹SpringBoot使用minio及配置代碼,感興趣的朋友一起看看吧
    2022-02-02
  • 基于Java代碼配置MyBatis Generator

    基于Java代碼配置MyBatis Generator

    這篇文章主要介紹了基于Java代碼配置MyBatis Generator,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知

    詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知

    本篇文章主要介紹了詳解Spring Boot Admin監(jiān)控服務(wù)上下線郵件通知,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • java并發(fā)編程JUC CountDownLatch線程同步

    java并發(fā)編程JUC CountDownLatch線程同步

    這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來展開對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • Java協(xié)議字節(jié)操作工具類詳情

    Java協(xié)議字節(jié)操作工具類詳情

    這篇文章主要介紹了Java協(xié)議字節(jié)操作工具類詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 構(gòu)建Java樹結(jié)構(gòu)的三種實(shí)現(xiàn)方法

    構(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ù)源切換的示例代碼

    本文主要介紹了Spring攔截器實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-08-08
  • 解讀Spring-boot的debug調(diào)試

    解讀Spring-boot的debug調(diào)試

    這篇文章主要介紹了解讀Spring-boot的debug調(diào)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享

    java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享

    這篇文章主要介紹了java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享,本文直接給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-03-03

最新評(píng)論

大渡口区| 望谟县| 灵石县| 大兴区| 全州县| 天门市| 区。| 苍南县| 恩平市| 略阳县| 眉山市| 涟源市| 浦城县| 香港| 云南省| 阳谷县| 迁安市| 冷水江市| 泰和县| 古交市| 蒙山县| 诸暨市| 拉孜县| 海安县| 湄潭县| 芮城县| 泉州市| 靖边县| 克山县| 梁平县| 嘉义县| 丹江口市| 读书| 阳春市| 孝义市| 婺源县| 信阳市| 普安县| 加查县| 高雄市| 寿宁县|