Object转JSON,子类读父类内容

public class ObjectUtil {

	/**
	 * 从父类对类中读数据
	 * @param parent
	 * @param subObj
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void readDataFromParent(Object parent, Object subObj){
		Class parrentClazz = parent.getClass();
		Method[] methods = parrentClazz.getMethods();
		try{
			for(Method method : methods){
				if(method.getName().startsWith("get") && !method.getName().startsWith("getClass")){
					Object value = method.invoke(parent);
					String setName = method.getName().replaceFirst("g", "s");
					Method subSetMethod = parrentClazz.getMethod(setName, method.getReturnType());
					subSetMethod.invoke(subObj, value);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	@SuppressWarnings({ "rawtypes" })
	public static JSONObject obj2Json(Object obj){
		if(obj == null){
			return null;
		}
		Class parrentClazz = obj.getClass();
		JSONObject ret = new JSONObject();
		Method[] methods = parrentClazz.getMethods();
		try{
			for(Method method : methods){
				if(method.getName().startsWith("get") && !method.getName().startsWith("getClass")){
					Object value = method.invoke(obj);
					if(value == null){
						continue;
					}
					String attrName = method.getName().substring("get".length());
					attrName = StringUtil.firstCharLower(attrName);
					if(isBasicType(value)){
						ret.put(attrName, value);
					}
					else if(value instanceof List){
						List list = (List)value;
						if(list != null){
							JSONArray ja = new JSONArray();
							for(Object varObj : list){
								if(!isBasicType(varObj)){
									ja.add(obj2Json(varObj));
								}
								else{
									ja.add(varObj);
								}
							}
							ret.put(attrName, ja);
						}
					}
					else{
						ret.put(attrName, obj2Json(value));
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return ret;
	}
	
	
	/**
	 * 判断是否是基础类型对象,我把String也当作了基础类型
	 * @param value
	 * @return
	 */
	private static boolean isBasicType(Object value){
		if(value.getClass() == int.class || value instanceof Integer){
			return true;
		}
		else if(value.getClass() == long.class || value instanceof Long){
			return true;
		}
		else if(value.getClass() == short.class || value instanceof Short){
			return true;
		}
		else if(value.getClass() == byte.class || value instanceof Byte){
			return true;
		}
		else if(value.getClass() == boolean.class || value instanceof Boolean){
			return true;
		}
		else if(value.getClass() == double.class || value instanceof Double){
			return true;
		}
		else if(value.getClass() == float.class || value instanceof Float){
			return true;
		}
		else if(value.getClass() == char.class || value instanceof Character){
			return true;
		}
		else if(value instanceof String){
			return true;
		}
		return false;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值