当前位置:网站首页>Object to map
Object to map
2022-04-23 05:42:00 【wyazyf】
/**
* Object turn map
* @param obj
* @return
*/
private static Map<String, Object> objToMap(Object obj) {
Map<String, Object> map = new HashMap<String, Object>();
Field[] fields = obj.getClass().getDeclaredFields(); // obtain f Object corresponds to all property fields in the class
for (int i = 0, len = fields.length; i < len; i++) {
String varName = fields[i].getName();
varName = varName.toLowerCase(); // take key Set to lowercase , The default is the attribute of the object
try {
boolean accessFlag = fields[i].isAccessible(); // Get the original access control permission
fields[i].setAccessible(true); // Modify access control permissions
Object o = fields[i].get(obj); // Get in object f Middle attribute fields[i] Variables in the corresponding object
if (o != null){
map.put(varName, o.toString());
}
fields[i].setAccessible(accessFlag); // Restore access control permissions
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
}
return map;
}
版权声明
本文为[wyazyf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230538215252.html
边栏推荐
猜你喜欢

Create cells through JS (while loop)

橙单微服务之批量导入

Generation of straightening body in 3D slicer

Pol / select / EPO

Issue 36 summary of atcoder beginer contest 248

创建进程内存管理copy_mm - 进程与线程(九)

Some pits used by uni

Find the number of "blocks" in the matrix (BFS)

Excel sets row and column colors according to cell contents

AcWing 836. Merge set (merge set)
随机推荐
C, class library
Edit, cancel, pull up menu
MySQL create Oracle exercise table
CMake基础教程(39)pkgconfig
Arithmetic and logical operations
Common protocols of OSI layer
Range of numbers (dichotomous classic template topic)
Flutter 新一代图形渲染器 Impeller
Use of qwbengneview and qwebchannel.
Create cells through JS (while loop)
合约锁仓漏洞
50 SQL exercises, answers and detailed analysis
Map object map get(key)
[machine learning] scikit learn introduction
ES6之解构函数
可执行程序执行流程
Rog attack
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
Introduction to qqueue
Pavlov and hobbies