当前位置:网站首页>对象转map
对象转map
2022-04-23 05:38:00 【wyazyf】
/**
* 对象转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(); // 获取f对象对应类中的所有属性域
for (int i = 0, len = fields.length; i < len; i++) {
String varName = fields[i].getName();
varName = varName.toLowerCase(); // 将key置为小写,默认为对象的属性
try {
boolean accessFlag = fields[i].isAccessible(); // 获取原来的访问控制权限
fields[i].setAccessible(true); // 修改访问控制权限
Object o = fields[i].get(obj); // 获取在对象f中属性fields[i]对应的对象中的变量
if (o != null){
map.put(varName, o.toString());
}
fields[i].setAccessible(accessFlag); // 恢复访问控制权限
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
}
return map;
}
版权声明
本文为[wyazyf]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wyazyf/article/details/110634318
边栏推荐
- ‘EddiesObservations‘ object has no attribute ‘filled‘
- Data mining -- understanding data
- Strategies to improve Facebook's touch rate and interaction rate | intelligent customer service helps you grasp users' hearts
- 【华为机试】考试得分总数(如何处理答错的情况?回溯一次,代表答错一题)
- Common interview questions - 4 (MySQL)
- STD:: String implements split
- poi导出excel,行相同数据自动合并单元格
- 引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
- Pytorch deep learning practice_ 11 convolutional neural network
- JSON.
猜你喜欢
Differences between sea level anatomy and sea surface height anatomy
‘EddiesObservations‘ object has no attribute ‘filled‘
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
Issue 36 summary of atcoder beginer contest 248
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
QT displays the specified position and size of the picture
C language - Spoof shutdown applet
Three methods of list rendering
SQL statement simple optimization
第36期《AtCoder Beginner Contest 248 打比赛总结》
随机推荐
Flutter 新一代图形渲染器 Impeller
Data mining -- understanding data
3d slicer中拉直体的生成
(11) Vscode code formatting configuration
Cmake basic tutorial (39) pkgconfig
Understand the relationship between promise async await
qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““
mysql中duplicate key update
Deconstruction function of ES6
Common protocols of OSI layer
Solid contract DoS attack
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
Qwebsocket communication
JSON.
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
Find the number of "blocks" in the matrix (BFS)
catkin_package到底干了什么
Edit, cancel, pull up menu
Intel SGX preliminary learning and understanding notes (continuously updated)
Traversal array, object parent-child communication props / $emit