2021-06-18 object转map
Posted IT的鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-06-18 object转map相关的知识,希望对你有一定的参考价值。
public class MapUtil {
public MapUtil() {
}
public static Map<String, Object> objectToMap(Object obj) {
if (obj == null) {
return null;
} else {
Map<String, Object> map = new HashMap();
Method[] methods = obj.getClass().getMethods();
Method[] var3 = methods;
int var4 = methods.length;
for(int var5 = 0; var5 < var4; ++var5) {
Method method = var3[var5];
try {
if (method.getName().startsWith("get")) {
String field = toFirstLower(method.getName().replace("get", ""));
Object objValue = method.invoke(obj, (Object[])null);
map.put(field, objValue);
}
} catch (Exception var12) {
log.error(var12);
}
}
for(Class superClass = obj.getClass().getSuperclass(); !superClass.getName().equals("java.lang.Object"); superClass = superClass.getSuperclass()) {
Method[] superMethods = superClass.getMethods();
Method[] var15 = superMethods;
int var16 = superMethods.length;
for(int var17 = 0; var17 < var16; ++var17) {
Method method = var15[var17];
try {
if (method.getName().startsWith("get")) {
String field = toFirstLower(method.getName().replace("get", ""));
Object objValue = method.invoke(obj, (Object[])null);
map.put(field, objValue);
}
} catch (Exception var11) {
log.error(var11);
}
}
}
return map;
}
}
private static String toFirstLower(String str) {
if (str != null && !"".equals(str)) {
char[] chars = new char[]{str.charAt(0)};
String temp = new String(chars);
return str.replaceFirst(temp, temp.toLowerCase());
} else {
return "";
}
}
}
public static void excleCommonUtil(List<Object> objectList, String title, String... exclusions) throws Exception {
if (!org.springframework.util.CollectionUtils.isEmpty(objectList)) {
ArrayList<Map> listMap = (ArrayList<Map>) objectList.get(0);
Map<String, Object> objectMap = com.huawei.it.jalor5.core.util.MapUtil.objectToMap(listMap.get(0));
List<String> rowNameList = new ArrayList<>();
if (!Objects.isNull(exclusions)) {
Iterator<Map.Entry<String, Object>> it = objectMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
rowNameList.add(entry.getKey());
for (String exclusion : exclusions) {
if (exclusion.equals(entry.getKey())) {
it.remove();
rowNameList.remove(entry.getKey());
}
}
}
}
String[] rowName = rowNameList.toArray(new String[0]);
List<Object[]> dataList = new ArrayList<>();
for (Object list : listMap) {
Object[] rowList = new String[rowNameList.size()];
Map<String, Object> objMap = MapUtil.objectToMap(list);
for (int i = 0; i < rowNameList.size(); i++) {
Object objectValue = objMap.get(rowNameList.get(i));
if (objectValue instanceof Date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectValue = formatter.format(objectValue);
}
rowList[i] = objectValue;
}
dataList.add(rowList);
}
Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletResponse response = null;
if (message.get(AbstractHTTPDestination.HTTP_RESPONSE) instanceof HttpServletResponse) {
response = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
}
if (response != null) {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
}
ExportExcelUtil excelUtil = new ExportExcelUtil(title, rowName, dataList);
excelUtil.export(null, response);
}
}
以上是关于2021-06-18 object转map的主要内容,如果未能解决你的问题,请参考以下文章
map怎么转成list<map<string,object>>
map<Integer,List>转String[]怎样转?