当前位置:网站首页>工具类:对象转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();
}
}
边栏推荐
- [shell] summary of common shell commands and test judgment statements
- string(讲解)
- Tkinter window selects PCD file and displays point cloud (open3d)
- Jetpack Compose 远不止是一个UI框架这么简单~
- SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
- C interview encryption program: input plaintext by keyboard, convert it into ciphertext through encryption program and output it to the screen.
- 分布式id解决方案
- Data of all class a scenic spots in China in 2022 (13604)
- dolphinscheduler3.x本地启动
- 企業如何進行數據治理?分享數據治理4個方面的經驗總結
猜你喜欢
随机推荐
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
Basic DOS commands
VIM mapping large K
C language (structure) defines a user structure with the following fields:
unity3d学习笔记
MySql用户权限
数据资产管理与数据安全国内外最新趋势
企业如何进行数据治理?分享数据治理4个方面的经验总结
MATLAB小技巧(30)非线性拟合 lsqcurefit
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
Jetpack Compose 远不止是一个UI框架这么简单~
【解决】Final app status- UNDEFINED, exitCode- 16
【NOI模拟赛】区域划分(结论,构造)
from .onnxruntime_pybind11_state import * # noqa ddddocr运行报错
品牌电商如何逆势增长?在这里预见未来!
大促过后,销量与流量兼具,是否真的高枕无忧?
How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet
Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
中英文说明书丨ProSci LAG-3 重组蛋白
[shell] summary of common shell commands and test judgment statements








