当前位置:网站首页>对象转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
边栏推荐
- what is wifi6?
- Wbpack configuring production development environment
- SQL语句简单优化
- 修仙真实世界与游戏世界
- Some pits used by uni
- Breadth first search topics (BFS)
- Create a tabbar component under the components folder, which is public
- qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““
- 热键,界面可视化配置(界面交互)
- Edit, cancel, pull up menu
猜你喜欢
Fast application fuzzy search
Three methods of list rendering
Create cells through JS (while loop)
mysql中duplicate key update
The title bar will be pushed to coincide with the status bar
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
Pytorch deep learning practice_ 11 convolutional neural network
Flutter 新一代圖形渲染器 Impeller
Establish excel bookkeeping book through setting context menu
Create process memory management copy_ Mm - processes and threads (IX)
随机推荐
uni使用的一些坑
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
What financial products will benefit during May Day?
QSS, qdateedit, qcalendarwidget custom settings
基于ssm 包包商城系统
Common protocols of OSI layer
第36期《AtCoder Beginner Contest 248 打比赛总结》
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
Issue 36 summary of atcoder beginer contest 248
五一劳动节期间什么理财产品会有收益?
Wbpack configuring production development environment
Double click The jar package cannot run the solution
Add two pointers? (legal or illegal)
数据安全入门产品——数据库审计系统详解
Executable program execution process
Use of qwbengneview and qwebchannel.
js数字大写方法
Frequently asked interview questions - 1 (non technical)
Usage and difference of shellexecute, shellexecuteex and winexec in QT
热键,界面可视化配置(界面交互)