菜鸟笔记
提升您的技术认知

map与java对象相互转换-ag真人游戏

最近,研究map与java对象之间的相互转换,总结了5种方法:

第一种:使用org.apache.commons.beanutils转换

用到的主要jar包:commons-beanutils-1.9.3.jar

//map转java对象
public static object maptoobject(map 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.(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
	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)
	... 16 more

解决方法见本人的另一篇文章: https://blog.csdn.net/liuyunyihao/article/details/86596595

第二种:使用introspector转换

//map转java对象
public static object maptoobject(map 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 objecttomap(object obj) throws exception {
	if (obj == null) {
		return null;
	}
	map map = new hashmap();
	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;
}

第三种:使用reflect转换

//map转java对象
public static object maptoobject(map 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 objecttomap(object obj) throws exception {
	if (obj == null) {
		return null;
	}
	map map = new hashmap();
	field[] declaredfields = obj.getclass().getdeclaredfields();
	for (field field : declaredfields) {
		field.setaccessible(true);
		map.put(field.getname(), field.get(obj));
	}
	return map;
}

第四种:使用net.sf.cglib.beans.beanmap转换

用到的主要jar包:cglib-2.2.2.jar

//map转java对象
public static object maptoobject(map map, class beanclass) throws exception {
    object object = beanclass.newinstance();
    beanmap beanmap = beanmap.create(object);
    beanmap.putall(map);
    return object;
}
//java对象转map
public static map objecttomap(object obj) {
    map 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.(keyfactory.java:66)
	at net.sf.cglib.beans.beanmap$generator.(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

解决方法见本人的另一篇文章:https://blog.csdn.net/liuyunyihao/article/details/86593722

第五种:使用json转换

用到的主要jar包:fastjson-1.2.54.jar

//map转java对象
public static object maptoobject(map map, class beanclass) throws exception {
    string jsonstr = jsonobject.tojsonstring(map);
    return jsonobject.parseobject(jsonstr, beanclass);
}
//java对象转map
public static map objecttomap(object obj) {
    string jsonstr = jsonobject.tojsonstring(obj);
    return jsonobject.parseobject(jsonstr);
}

以上方法本人均实际测试通过。

网站地图