將Object對象轉換成Map 屬性名和值的形式

package cn.lonelcoud.util;
 
 import com.sun.deploy.util.StringUtils;
 
 import java.lang.reflect.Field;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
 /**
  * Created by lonecloud on 17/3/12.
  * 用於對Object進行解析並且轉換成Map鍵值對的形式
  *
  * @author lonecloud
  * @version 1.0
  */
 public class ObjectUtils {
 
     private static final String JAVAP = "java.";
     private static final String JAVADATESTR = "java.util.Date";
 
     /**
      * 獲取利用反射獲取類裏面的值和名稱
      *
      * @param obj
      * @return
      * @throws IllegalAccessException
      */
     public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
         Map<String, Object> map = new HashMap<>();
         Class<?> clazz = obj.getClass();
         System.out.println(clazz);
         for (Field field : clazz.getDeclaredFields()) {
             field.setAccessible(true);
             String fieldName = field.getName();
             Object value = field.get(obj);
             map.put(fieldName, value);
         }
         return map;
     }
 
     /**
      * 利用遞歸調用將Object中的值全部進行獲取
      *
      * @param timeFormatStr 格式化時間字符串默認<strong>2017-03-10 10:21</strong>
      * @param obj           對象
      * @param excludeFields 排除的屬性
      * @return
      * @throws IllegalAccessException
      */
     public static Map<String, String> objectToMapString(String timeFormatStr, Object obj, String... excludeFields) throws IllegalAccessException {
         Map<String, String> map = new HashMap<>();
 
         if (excludeFields.length!=0){
             List<String> list = Arrays.asList(excludeFields);
             objectTransfer(timeFormatStr, obj, map, list);
         }else{
             objectTransfer(timeFormatStr, obj, map,null);
         }
         return map;
     }
 
     /**
      * 遞歸調用函數
      *
      * @param obj           對象
      * @param map           map
      * @param excludeFields 對應參數
      * @return
      * @throws IllegalAccessException
      */
     private static Map<String, String> objectTransfer(String timeFormatStr, Object obj, Map<String, String> map, List<String> excludeFields) throws IllegalAccessException {
         boolean isExclude=false;
         //默認字符串
         String formatStr = "YYYY-MM-dd HH:mm:ss";
         //設置格式化字符串
         if (timeFormatStr != null && !timeFormatStr.isEmpty()) {
             formatStr = timeFormatStr;
         }
         if (excludeFields!=null){
             isExclude=true;
         }
         Class<?> clazz = obj.getClass();
         //獲取值
         for (Field field : clazz.getDeclaredFields()) {
             String fieldName = clazz.getSimpleName() + "." + field.getName();
             //判斷是不是需要跳過某個屬性
             if (isExclude&&excludeFields.contains(fieldName)){
                 continue;
             }
             //設置屬性可以被訪問
             field.setAccessible(true);
             Object value = field.get(obj);
             Class<?> valueClass = value.getClass();
             if (valueClass.isPrimitive()) {
                 map.put(fieldName, value.toString());
 
             } else if (valueClass.getName().contains(JAVAP)) {//判斷是不是基本類型
                 if (valueClass.getName().equals(JAVADATESTR)) {
                     //格式化Date類型
                     SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
                     Date date = (Date) value;
                     String dataStr = sdf.format(date);
                     map.put(fieldName, dataStr);
                 } else {
                     map.put(fieldName, value.toString());
                 }
             } else {
                 objectTransfer(timeFormatStr, value, map,excludeFields);
             }
         }
         return map;
     }
 
 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章