Person person =new Person();
person.setAdult(true);
person.setName("1");
// 将对象转换成json字符串
String r = JSONUtil.parseObj(person, false).toString();
System.out.println(r);
将对象转换成 map对象
Map<String, Object> stringObjectMap = objectToMap(person);
System.out.println(stringObjectMap);
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = field.get(obj) == null ? "":field.get(obj);
map.put(fieldName, value);
}
return map;
}