將Java物件轉換成Map的鍵值對形式
程式碼:
1 package cn.lonelcoud.util; 2 3 import com.sun.deploy.util.StringUtils; 4 5 import java.lang.reflect.Field; 6 import java.text.SimpleDateFormat; 7 import java.util.*; 8 9 /** 10 * Created by lonecloud on 17/3/12. 11 * 用於對Object進行解析並且轉換成Map鍵值對的形式 12 * 13 * @author lonecloud 14 * @version 1.0 15 */ 16 public class ObjectUtils { 17 18 private static final String JAVAP = "java."; 19 private static final String JAVADATESTR = "java.util.Date"; 20 21 /** 22 * 獲取利用反射獲取類裡面的值和名稱 23 * 24 * @param obj 25 * @return 26 * @throws IllegalAccessException 27 */ 28 public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException { 29 Map<String, Object> map = new HashMap<>(); 30 Class<?> clazz = obj.getClass(); 31 System.out.println(clazz); 32 for (Field field : clazz.getDeclaredFields()) { 33 field.setAccessible(true); 34 String fieldName = field.getName(); 35 Object value = field.get(obj); 36 map.put(fieldName, value); 37 } 38 return map; 39 } 40 41 /** 42 * 利用遞迴呼叫將Object中的值全部進行獲取 43 * 44 * @param timeFormatStr 格式化時間字串預設<strong>2017-03-10 10:21</strong> 45 * @param obj 物件 46 * @param excludeFields 排除的屬性 47 * @return 48 * @throws IllegalAccessException 49 */ 50 public static Map<String, String> objectToMapString(String timeFormatStr, Object obj, String... excludeFields) throws IllegalAccessException { 51 Map<String, String> map = new HashMap<>(); 52 53 if (excludeFields.length!=0){ 54 List<String> list = Arrays.asList(excludeFields); 55 objectTransfer(timeFormatStr, obj, map, list); 56 }else{ 57 objectTransfer(timeFormatStr, obj, map,null); 58 } 59 return map; 60 } 61 62 63 /** 64 * 遞迴呼叫函式 65 * 66 * @param obj 物件 67 * @param map map 68 * @param excludeFields 對應引數 69 * @return 70 * @throws IllegalAccessException 71 */ 72 private static Map<String, String> objectTransfer(String timeFormatStr, Object obj, Map<String, String> map, List<String> excludeFields) throws IllegalAccessException { 73 boolean isExclude=false; 74 //預設字串 75 String formatStr = "YYYY-MM-dd HH:mm:ss"; 76 //設定格式化字串 77 if (timeFormatStr != null && !timeFormatStr.isEmpty()) { 78 formatStr = timeFormatStr; 79 } 80 if (excludeFields!=null){ 81 isExclude=true; 82 } 83 Class<?> clazz = obj.getClass(); 84 //獲取值 85 for (Field field : clazz.getDeclaredFields()) { 86 String fieldName = clazz.getSimpleName() + "." + field.getName(); 87 //判斷是不是需要跳過某個屬性 88 if (isExclude&&excludeFields.contains(fieldName)){ 89 continue; 90 } 91 //設定屬性可以被訪問 92 field.setAccessible(true); 93 Object value = field.get(obj); 94 Class<?> valueClass = value.getClass(); 95 if (valueClass.isPrimitive()) { 96 map.put(fieldName, value.toString()); 97 98 } else if (valueClass.getName().contains(JAVAP)) {//判斷是不是基本型別 99 if (valueClass.getName().equals(JAVADATESTR)) { 100 //格式化Date型別 101 SimpleDateFormat sdf = new SimpleDateFormat(formatStr); 102 Date date = (Date) value; 103 String dataStr = sdf.format(date); 104 map.put(fieldName, dataStr); 105 } else { 106 map.put(fieldName, value.toString()); 107 } 108 } else { 109 objectTransfer(timeFormatStr, value, map,excludeFields); 110 } 111 } 112 return map; 113 } 114 115 }
該程式碼共享於github中 github地址