依赖阿里的FastJSon,用到的主要jar包:fastjson-1.2.54.jar
1、maven引入
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
2、类引入
import com.alibaba.fastjson.JSON;
3、转换
简单使用
//map转对象
User user = JSON.parseObject(JSON.toJSONString(map), User.class);
//对象转map
Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(user));
封装提取方法
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> bean) throws Exception {
String jsonStr = JSONObject.toJSONString(map);
return JSONObject.parseObject(jsonStr, Bean.Class);
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) {
String jsonStr = JSONObject.toJSONString(obj);
return JSONObject.parseObject(jsonStr, Map.Class);
}
用到的主要jar包:commons-beanutils-1.9.3.jar
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass)
throws Exception {
if (map == null) {
return null;
}
Object obj = beanClass.newInstance();
BeanUtils.populate(obj, map);
return obj;
}
//java对象转map
public static Map<?, ?> objectToMap(Object obj) {
if (obj == null)
return null;
return new BeanMap(obj);
}
在使用该方法进行java对象转map时,可能会发生以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at org.apache.commons.beanutils.BeanMap.createTypeTransformers(BeanMap.java:132)
at org.apache.commons.beanutils.BeanMap.<clinit>(BeanMap.java:68)
at com.example2.pojo.PojoToMap.objectToMap(PojoToMap.java:23)
at com.example2.pojo.PojoToMap.main(PojoToMap.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer
...
从报错信息上看,是缺少了org.apache.commons.collections.Transformer类,网上下载commons-collections-3.2.1.jar包,引入后,重新执行,成功
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;
Object obj = beanClass.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
setter.invoke(obj, map.get(property.getName()));
}
}
return obj;
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
map.put(key, value);
}
return map;
}
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null) {
return null;
}
Object object = beanClass.newInstance();
Field[] fields = beanClass.getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isFinal(mod) || Modifier.isStatic(mod)) {
continue;
}
field.setAccessible(true);
field.set(object, map.get(field.getName()));
}
return object;
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
return map;
}
用到的主要jar包:cglib-2.2.2.jar
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
Object object = beanClass.newInstance();
BeanMap beanMap = BeanMap.create(object);
beanMap.putAll(map);
return object;
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) {
Map<String, Object> map = Maps.newHashMap();
if (obj != null) {
BeanMap beanMap = BeanMap.create(obj);
for (Object key : beanMap.keySet()) {
map.put(key + "", beanMap.get(key));
}
}
return map;
}
在使用本方法进行map转java对象时,可能会发生以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/Type
at net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:184)
at net.sf.cglib.core.KeyFactory.<clinit>(KeyFactory.java:66)
at net.sf.cglib.beans.BeanMap$Generator.<clinit>(BeanMap.java:64)
at net.sf.cglib.beans.BeanMap.create(BeanMap.java:56)
at com.example2.pojo.MapToPojo.mapToBean(MapToPojo.java:27)
at com.example2.pojo.MapToPojo.main(MapToPojo.java:18)
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 6 more
从错误信息上看,是缺少了org.objectweb.asm.Type类,从网上下载了com.springsource.org.objectweb.asm-3.2.0.jar包,引入后,再次执行,成功