将java对象属性转化为Map集合

package com.bpcrm.util;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.bpcrm.customer.form.CustomerSendForm;

public class ObjectUtils {
	/**
	 * 将指定对象属性名称和属性值转化为Map键值对
	 * 
	 * @param obj
	 * @return
	 */
	public static HashMap objectToMap(Object obj) throws Exception {
		if (obj == null) {
			throw new Exception("对象为空");
		}

		Class clazz = obj.getClass();
		HashMap map = new HashMap();
    getClass(clazz,map,obj);
		HashMap newMap = convertHashMap(map);
		return newMap;
	}

	/**
	 * 带入一个对象,一个对象中的属性名称,取得该属性的值
	 * @param obj
	 * @param strProperty
	 * @return
	 * @throws Exception
	 */
	public static String getPropertyValueFormObject(Object obj,
			String strProperty) throws Exception  {
		if (!StringUtils.isValidateString(strProperty)) {
			return "";
		}
		if (obj == null) {
			return "";
		}
		Class clazz = obj.getClass();
		HashMap map = new HashMap();
    getClass(clazz,map,obj);
		HashMap newMap = convertHashMap(map);

		if (newMap == null) {
			return "";
		} else {
			Object objReturn = newMap.get(StringUtils.validateString(strProperty));
			if (objReturn==null) {
				return "";
			} else {
				return objReturn.toString();
			}

		}

	}
	/**
	 *
	*/
	private static void getClass(Class clazz,HashMap map,Object obj) throws Exception{
		if(clazz.getSimpleName().equals("Object")){
			return;
		}
		
	Field[] fields = clazz.getDeclaredFields();
		if (fields == null || fields.length <= 0) {
			throw new Exception("当前对象中没有任何属性值");
		}
		for(int i=0;i<fields.length;i++){
			fields[i].setAccessible(true);
			String name=fields[i].getName();
			Object value=fields[i].get(obj);
			map.put(name,value);
			
		}
		Class superClzz=clazz.getSuperclass();
		getClass(superClzz,map,obj);
	}
	/**
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	private static HashMap convertHashMap(HashMap map) throws Exception {

		HashMap newMap = new HashMap();
		Set keys = map.keySet();
		Iterator it = keys.iterator();
		while (it.hasNext()) {
			Object key = it.next();
			convertToString(map.get(key), newMap, key);
		}

		return newMap;
	}

	/**
	 * 
	 * @param value
	 * @param newMap
	 * @param key
	 */
	private static void convertToString(Object value, HashMap newMap, Object key) {
		if (value != null) {
			Class clazz = value.getClass();
			if (isBaseType(clazz)) {
				newMap.put(key, value.toString());
			} else if (clazz == String.class) {
				newMap.put(key, value.toString());
			} else if (clazz == Date.class) {
				Date date = (Date) value;
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				newMap.put(key, sdf.format(date));
			} else if (clazz == Timestamp.class) {
				Timestamp timestamp = (Timestamp) value;
				long times = timestamp.getTime();
				Date date = new Date(times);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				newMap.put(key, sdf.format(date));
			} else if (clazz == java.sql.Date.class) {
				java.sql.Date sqlDate = (java.sql.Date) value;
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				newMap.put(key, sdf.format(sqlDate));
			} else {
				newMap.put(key, value);
			}
		} else {
			newMap.put(key, value);
		}

	}

	/**
	 * 
	 * @param clazz
	 * @return
	 */
	private static boolean isBaseType(Class clazz) {

		if (clazz == Integer.class) {
			return true;
		}
		if (clazz == Long.class) {
			return true;
		}
		if (clazz == Double.class) {
			return true;
		}
		if (clazz == Byte.class) {
			return true;
		}
		if (clazz == Float.class) {
			return true;
		}
		if (clazz == Short.class) {
			return true;
		}
		if (clazz == Boolean.class) {
			return true;
		}
		return false;
	}

	/**
	 * 测试
	 * 
	 * @param args
	 */
	public static void main(String args[]) throws Exception {
		System.out.println("132131");
		CustomerSendForm user = new CustomerSendForm();
		// user.setAge(1);
		user.setId("1");
		// user.setName("qqqq");
		// user.setBirthday(new Date());
		// user.setFriedns(new ArrayList());
		// user.setHobbies(new String[]{"1","2"});
		HashMap map = objectToMap(user);
		Set entrys = map.entrySet();
		Iterator it = entrys.iterator();
		System.out.println("sfsdfsdf");
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值