隐藏页面特效

将Object对象转换成Map 属性名和值的形式

将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地址


__EOF__

本文作者lonecloud
本文链接https://www.cnblogs.com/lonecloud/p/6537586.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   lonecloud  阅读(57617)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:lonecloud,邀请大家一同入驻:https://www.oschina.net/sharing-plan/apply
点击右上角即可分享
微信分享提示