int code = 0;
//接收Object对象
Object date = resultMsg.getData();
//通过DateObject的Object对象,获取运行时类的对象
Class jsonClass = date.getClass();
//Field用于获取某个类的属性或该属性的属性值
Field dataField = null;
try {
//获取object中的code属性
dataField = jsonClass.getDeclaredField("code");
//设置code属性为可访问的
dataField.setAccessible(true);
//声明Object对应实体类
DateObject tempSubEvent= new DateObject();
try {
//通过Field.get(Object)获取object的data(SubEvent)中的eventId属性
tempSubEvent = (DateObject) dataField.get(date);
code = tempSubEvent.getCode();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
反射取属性值
最新推荐文章于 2024-08-16 23:08:28 发布