The form of a key-value pair that converts a Java object into a map
Code:
Package cn.lonelcoud.util;
Import Com.sun.deploy.util.StringUtils;
Import Java.lang.reflect.Field;
Import Java.text.SimpleDateFormat;
Import java.util.*;
/** * Created by Lonecloud on 17/3/12. * For parsing object and converting it into map key value pairs * * @author Lonecloud * @version 1.0/public class Objectutils {private static F
inal String javap = "java.";
private static final String javadatestr = "Java.util.Date"; /** * Gets the value and name inside the class using reflection get * * @param obj * @return * @throws illegalaccessexception/Publ IC static map<string, object> objecttomap (Object obj) throws illegalaccessexception {map<string, object& Gt
Map = new hashmap<> ();
Class<?> clazz = Obj.getclass ();
System.out.println (Clazz);
For (Field Field:clazz.getDeclaredFields ()) {field.setaccessible (true);
String fieldName = Field.getname ();
Object value = field.get (obj); Map.put (FieldName, value);
} return map; /** * Use recursive calls to get all of the values in Object * @param timeformatstr format time string default <strong>2017-03-10 10:21</s trong> * @param obj Object * @param excludefields excluded Properties * @return * @throws illegalaccessexce Ption */public static map<string, string> objecttomapstring (string timeformatstr, Object obj, string ... ex
Cludefields) throws Illegalaccessexception {map<string, string> Map = new hashmap<> ();
if (excludefields.length!=0) {list<string> List = arrays.aslist (excludefields);
Objecttransfer (timeformatstr, obj, map, list);
}else{Objecttransfer (timeformatstr, obj, map,null);
} return map; /** * Recursive call function * @param obj Object * @param map map * @param Excludefield s corresponding parameter * @return * @throws illegalaccessexception/private static Map<sTring, string> objecttransfer (String timeformatstr, Object obj, map<string, string> Map, list<string>
Excludefields) throws Illegalaccessexception {Boolean isexclude=false;
Default string formatstr = "Yyyy-mm-dd HH:mm:ss";
Sets the format string if (timeformatstr!= null &&!timeformatstr.isempty ()) {formatstr = Timeformatstr;
} if (excludefields!=null) {isexclude=true;
} class<?> Clazz = Obj.getclass (); Gets the value for (Field field:clazz.getDeclaredFields ()) {String fieldName = clazz.getsimplename () + "." +
Field.getname ();
Determine if you need to skip a property if (Isexclude&&excludefields.contains (FieldName)) {continue;
}//Setting property can be accessed field.setaccessible (true);
Object value = field.get (obj);
class<?> Valueclass = Value.getclass (); if (valueclass. Isprimitive ()) {Map.put (FieldName, value.tostring ()); else if (Valueclass.getname (). Contains (JAVAP)) {//Determine whether the base type if (Valueclass.getname (). Equals (JAVADATESTR)
{//Format date type SimpleDateFormat SDF = new SimpleDateFormat (FORMATSTR);
Date date = (date) value;
String datastr = Sdf.format (date);
Map.put (FieldName, DATASTR);
else {map.put (fieldName, value.tostring ());
} else {Objecttransfer (timeformatstr, value, map,excludefields);
} return map; }
}
The code is shared in the GitHub GitHub address