Json字符串转Map
public static Map<String, Object> jsonToMap(Object object) {
if (object == null) {
return null;
}
JSONObject jsonObject = JSON.parseObject(object.toString());
if (jsonObject == null) {
return null;
}
Map<String, Object> data = new HashMap<String, Object>();
Iterator<String> it = jsonObject.keySet().iterator();
while (it.hasNext()) {
String key = String.valueOf(it.next());
Object value = jsonObject.get(key);
data.put(key, value);
}
return data;
}
Object对象转Map
public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
if (field.getType().isInstance(Map.class)) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = field.get(obj);
if (value == null) {
value = "";
}
map.put(fieldName, value);
}
}
return map;
}