java bean 轉 map

    /**
	 * - bean 2 map
	 * @param objList
	 * @return  
	 * @author: 
	 * @date: 
	 */
	@SuppressWarnings("rawtypes")
	public static List<Map<String, Object>> transBean2Map(List objList) {
		if (objList == null) {
			return null;
		}
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (Object obj : objList) {
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
				PropertyDescriptor[] propertyDescriptors = beanInfo
						.getPropertyDescriptors();
				for (PropertyDescriptor property : propertyDescriptors) {
					String key = property.getName();
					// 過濾class屬性
					if (!key.equals("class")) {
						// 得到property對應的getter方法
						Method getter = property.getReadMethod();
						Object value = getter.invoke(obj);

						map.put(key, value);
					}
				}
				result.add(map);
			} catch (Exception e) {
				logger.error("transBean2Map Error ", e);
			}
		}
		return result;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章