当前位置:网站首页>工具类:对象转map 驼峰转下划线 下划线转驼峰
工具类:对象转map 驼峰转下划线 下划线转驼峰
2022-07-07 02:40:00 【溜达的大象】
package com.esint.gaplatform.sjxxdataxjclgs.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Obj2map {
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<String, Object>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
String field_name = humpToLine(fieldName);
Object value = field.get(obj);
map.put(field_name, value);
}
return map;
}
private static Pattern linePattern = Pattern.compile("_(\\w)");
private static Pattern humpPattern = Pattern.compile("[A-Z]");
/** * 驼峰转下划线,最后转为大写 * @param str * @return */
public static String humpToLine(String str) {
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString().toUpperCase();
}
/** * 下划线转驼峰,正常输出 * @param str * @return */
public static String lineToHump(String str) {
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
}
边栏推荐
- dolphinscheduler3. X local startup
- Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
- Redis (II) - redis General Command
- 多个kubernetes集群如何实现共享同一个存储
- 基于JS的迷宫小游戏
- Install mongodb database
- 2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
- Stack and queue-p79-10 [2014 unified examination real question]
- 化工园区危化品企业安全风险智能化管控平台建设四大目标
- 企業如何進行數據治理?分享數據治理4個方面的經驗總結
猜你喜欢
![Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]](/img/13/096857158c9f977f8677f7cd0f9d4b.png)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]

使用TCP/IP四层模型进行网络传输的基本流程

Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition

Learning notes | data Xiaobai uses dataease to make a large data screen

偏执的非合格公司

POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...

Leite smart home longhaiqi: from professional dimming to full house intelligence, 20 years of focus on professional achievements

dolphinscheduler3. X local startup

JWT的基础介绍

反射(二)
随机推荐
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
MYSQL binlog相关命令
How to find the literature of a foreign language journal?
ceres-solver和g2o性能比较
数据资产管理与数据安全国内外最新趋势
MATLAB小技巧(30)非线性拟合 lsqcurefit
Performance comparison between Ceres solver and g2o
string(讲解)
MySQL (x)
mobx 知识点集合案例(快速入门)
如何给目标机器人建模并仿真【数学/控制意义】
UIC (configuration UI Engineering) public file library adds 7 industry materials
Leite smart home longhaiqi: from professional dimming to full house intelligence, 20 years of focus on professional achievements
Can't you really do it when you are 35 years old?
使用TCP/IP四层模型进行网络传输的基本流程
循环肿瘤细胞——Abnova 解决方案来啦
【解决】Final app status- UNDEFINED, exitCode- 16
HKUST & MsrA new research: on image to image conversion, fine tuning is all you need
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Data of all class a scenic spots in China in 2022 (13604)