當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Java笔记-通过放射获取类中成员名及调用get方法及map构造JSON数据
生活随笔
收集整理的這篇文章主要介紹了
Java笔记-通过放射获取类中成员名及调用get方法及map构造JSON数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
?
?
通過反射獲取類中成員名
通過反射調(diào)用get方法
map生成JSON數(shù)據(jù)
?
通過反射獲取類中成員名
/*** 獲取類中的所有成員名* @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; }?
通過反射調(diào)用get方法
/*** 調(diào)用此對(duì)象中所有g(shù)et方法并且返回?cái)?shù)組* @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數(shù)據(jù)
通過使用注解的方式做是很好的方式:
這里使用了data注解:
直接放進(jìn)去即可:
?
?
總結(jié)
以上是生活随笔為你收集整理的Java笔记-通过放射获取类中成员名及调用get方法及map构造JSON数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet文档阅读笔记-Extend
- 下一篇: C++笔记-使用std::funcion