Object to Map 转换的三种方法
就是把对象以及其对应的属性名 和属性值, 映射到一个Map中
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class ObjectToMap {
public static void main(String[] args) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
TestObject o = new TestObject();
o.setCity("beijing");
o.setName("bj");
o.setOrder("ASC");
ObjectToMap objectToMap = new ObjectToMap();
System.out.println("=========== Invoke objectToMapViaBeanInfo ===========");
objectToMap.objectToMapViaBeanInfo(o);
System.out.println("=========== Invoke objectToMapViaFields ===========");
objectToMap.objectToMapViaFields(o);
System.out.println("=========== Invoke objectToMapViaApacheTools ===========");
objectToMapViaApacheTools(o);
}
/**
* 通过Introspector 的方式复制属性值
* */
private static void objectToMapViaBeanInfo(TestObject o) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
Map<String, Object> result = new HashMap<String, Object>();
BeanInfo info = Introspector.getBeanInfo(o.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
//内容为null的过滤掉
if (reader == null || reader.invoke(o) == null) {
continue;
}
//默认继承Object类的属性,过滤掉
if (pd.getName().equalsIgnoreCase("class")) {
continue;
}
result.put(pd.getName(), reader.invoke(o));
}
for (String key : result.keySet()) {
System.out.println("Key:" + key + ";Value:" + result.get(key));
}
}
/**
* 通过getDeclaredFields 的方式复制属性值
* getDeclaredFields方式不会返回父类的属性
* */
private static void objectToMapViaFields(TestObject o) throws IllegalAccessException {
Map<String, Object> resMap = new HashMap<String, Object>();
Field[] declaredFields = o.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
//过滤内容为空的
if (field.get(o) == null) {
continue;
}
resMap.put(field.getName(), field.get(o));
}
printOut(resMap);
}
/**
* 通过Apache beanUtils的方式复制属性值
* 其内部实现方式同 方法1
* */
private static void objectToMapViaApacheTools(TestObject o) {
Map<String, Object> resMap = new org.apache.commons.beanutils.BeanMap(o);
printOut(resMap);
}
/**
* 打印结果
* */
private static void printOut(Map<String, Object> resMap) {
for (String key : resMap.keySet()) {
System.out.println("Key:" + key + ";Value:" + resMap.get(key));
}
}
/**
* 测试类
* */
public static class TestObject {
String city;
String name;
String order;
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
}
===================更新===================:
突然想不起来这种情况的应用场景了。那么是否可以转成JSONObject呢。
Student student = new Student();
JSONObject jsonObject =(JSONObject) JSONObject.toJSON(student);
System.out.print(jsonObject.toJSONString());
1、是为了转成字符串输出或存储?