當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Java笔记-通过放射获取类中成员名及调用get方法及map构造JSON数据
生活随笔
收集整理的這篇文章主要介紹了
Java笔记-通过放射获取类中成员名及调用get方法及map构造JSON数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
通過反射獲取類中成員名
通過反射調用get方法
map生成JSON數據
?
通過反射獲取類中成員名
/*** 獲取類中的所有成員名* @param currentClass* @return* @throws IllegalAccessException */ public static List<String> getMemberName(Class currentClass) throws IllegalAccessException {List<String> lists = new ArrayList<>();Field[] fields = currentClass.getDeclaredFields();for(Field field : fields){lists.add(field.getName());}return lists; }?
通過反射調用get方法
/*** 調用此對象中所有get方法并且返回數組* @param obj* @return* @throws IntrospectionException* @throws IllegalAccessException* @throws InstantiationException* @throws InvocationTargetException */ public static List<String> getReadFunction(Object obj) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {List<String> lists = new ArrayList<>();Class currentClass = obj.getClass();Field[] fields = currentClass.getDeclaredFields();for(Field field : fields){PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), currentClass);Method method = propertyDescriptor.getReadMethod();lists.add(method.invoke(obj).toString());}return lists; }?
?
map生成JSON數據
通過使用注解的方式做是很好的方式:
這里使用了data注解:
直接放進去即可:
?
?
總結
以上是生活随笔為你收集整理的Java笔记-通过放射获取类中成员名及调用get方法及map构造JSON数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet文档阅读笔记-Extend
- 下一篇: Spring中3种实例Bean的方法及设