public<T> Map<String, Object>toMapByJson(T obj){// 默认序列化为数字类型的时间戳// String jsonStr = JSON.toJSONString(obj);// Fastjson内置了一个默认的日期格式yyyy-MM-dd HH:mm:ss,// 可以通过在调用JSON.toJSONString时传入SerializerFeature.WriteDateUseDateFormat来启用。// 通过修改默认的时间...
private final static SimpleDateFormat yMdHms_sdf_util = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static Map<String, Object> convertBeanToMap(Object obj) { if (obj == null) { return null; } Map<String, Object> map = new HashMap<String, Object>(); try { BeanInfo beanInfo...
下面是一个完整的Java实体类对象转Map的示例代码: importjava.lang.reflect.Field;importjava.util.HashMap;importjava.util.Map;publicclassEntityToMapConverter{publicstaticMap<String,Object>convert(Objectentity)throwsIllegalAccessException{Map<String,Object>map=newHashMap<>();Class<?>entityClass=entity.getClas...
* 实体类转map对象 */publicstaticMap<String,Object>entityToMap(Objectentity){Map<String,Object>map=newHashMap<>();if(entity!=null){Field[]fields=entity.getClass().getDeclaredFields();for(Fieldfield:fields){field.setAccessible(true);try{map.put(field.getName(),field.get(entity));}catch(Ille...
public static void main(String[] args) { long s1 = System.currentTimeMillis(); Map<String, Object> dataMap = objectToMap(new User().setUser()); long s2 = System.currentTimeMillis(); System.out.println("花费时间毫秒:"+(s2-s1)); ...
public static void main(String[] args) throws Exception { // 创建一个Java Bean对象 Person person = new Person("张三", 25); // 手动实现将Java Bean对象转换为Map对象的方法 Map<String, Object> map = new HashMap<>(); Class<?> clazz = person.getClass(); ...
public class Main { public static void main(String[] args) throws Exception { User user = new User("Tom", 25); // 使用反射方法转换 Map<String, Object> userMap = EntityToMapConverter.convertToMap(user); System.out.println("User Map: " + userMap); // 使用Jackson方法转换 // ...
privatestatic<T>Map<String,Object>beanToMap(T bean){Map<String,Object>map=Maps.newHashMap();if(bean!=null){BeanMap beanMap=BeanMap.create(bean);for(Object key:beanMap.keySet()){map.put(key+"",beanMap.get(key));}}returnmap;
public static <T> Map<String, Object> entityToMap(T entity) { return new BeanMap(entity); } } ``` 上述方法使用了BeanMap类,将实体类转换为Map类型。BeanMap类是commons-beanutils库提供的一个工具类,它可以将JavaBean对象转换为Map类型。我们只需要将实体类作为参数传入BeanMap的构造方法,即可获得对应的...
Map<String, Object> map = new HashMap(); for (Field field : object.getClass().getDeclaredFields()){ try { boolean flag = field.isAccessible(); field.setAccessible(true); Object o = field.get(object); map.put(field.getName(), o); ...