声明:该版本是没完成的。该博文只做记录代码用
String memberType(String name) throws Exception
{
return getType(getClass().getField(name));
} public static DiaryInfo parse(JSONObject data) {
if (data == null) return null;
DiaryInfo info = new DiaryInfo();
String name;
Method action;
Object value;
Iterator<String> keys = data.keys();
try {
while (keys.hasNext()) {
name = keys.next();
value = data.get(name);
action = info.getClass().getMethod("set" + ZHelper.ucfirst(name), String.class);
action.invoke(info.getClass(), value);
}
} catch (JSONException e) {
AppException.json(e);
} catch (NoSuchMethodException e) {
AppException.run(e);
} catch (InvocationTargetException e) {
AppException.run(e);
} catch (IllegalArgumentException e) {
AppException.run(e);
} catch (IllegalAccessException e) {
AppException.run(e);
}
return info;
}
对JAVA 没什么经验,弄出了上面的代码,但是被 invoke 的参数类型郁闷,本来是想写个同名 Object 参数的 set 方法,然后自己转换数据类型,但觉得太麻烦了。后来我又想通过获取类中的 成员变量的类型来动态给 invoke 的参数值,但发现搞不定,很费时间,还是只有先 if else if 了,不浪费时间了,以后有经验了再来重写。