以下代码,可以将任何对象转换一个Map,字段的名字就是Map 的 key 值: 

import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * @author jiashubing
 * @since 2020/5/13
 */
public class BingTest {
    public static HashMap<String, Object> bingReflex(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        HashMap<String, Object> map = new HashMap<>();
        for (Field field : fields) {
            ReflectionUtils.makeAccessible(field);
            Object value = ReflectionUtils.getField(field, obj);
            if (value != null)
                map.put(field.getName(), value);
        }
        return map;
    }
}

 

相关文章:

  • 2021-12-19
  • 2021-05-14
  • 2022-03-06
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-24
猜你喜欢
  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案