欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java代码实现Map和Object互转及Map和Json互转

程序员文章站 2024-03-11 13:55:19
先给大家介绍下map和object互相转换的代码。 具体代码如所示: /** * 使用org.apache.commons.beanutils进行转换...

先给大家介绍下map和object互相转换的代码。

具体代码如所示:

/** 
* 使用org.apache.commons.beanutils进行转换 
*/ 
class a { 
public static object maptoobject(map<string, object> map, class<?> beanclass) throws exception { 
if (map == null) 
return null; 
object obj = beanclass.newinstance(); 
org.apache.commons.beanutils.beanutils.populate(obj, map); 
return obj; 
} 
public static map<?, ?> objecttomap(object obj) { 
if(obj == null) 
return null; 
return new org.apache.commons.beanutils.beanmap(obj); 
} 
} 
/** 
* 使用introspector进行转换 
*/ 
class b { 
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; 
} 
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; 
} 
} 
/** 
* 使用reflect进行转换 
*/ 
class c { 
public static object maptoobject(map<string, object> map, class<?> beanclass) throws exception { 
if (map == null) 
return null; 
object obj = beanclass.newinstance(); 
field[] fields = obj.getclass().getdeclaredfields(); 
for (field field : fields) { 
int mod = field.getmodifiers(); 
if(modifier.isstatic(mod) || modifier.isfinal(mod)){ 
continue; 
} 
field.setaccessible(true); 
field.set(obj, map.get(field.getname())); 
} 
return obj; 
} 
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; 
} 
<p>} </p><p> 
</p><p>from:http://www.open-open.com/code/view/1423280939826</p> 

下面给大家介绍map和json的互相转换

第一段代码

map<string,object> map = new hashmap<string,object>();
map.put("method","json");
map.put("param",null);
map.put("time","2015-01-23 10:54:55");
objectmapper mapper = new objectmapper();
mapper.writevalueasstring(map);

第二段代码

public static void readjson2map(string json) {
objectmapper objectmapper = new objectmapper();
try {
//将json字符串转成map结合解析出来,并打印(这里以解析成map为例)
map<string, map<string, object>> maps = objectmapper.readvalue(
json, map.class);
system.out.println(maps.size());
set<string> key = maps.keyset();
iterator<string> iter = key.iterator();
while (iter.hasnext()) {
string field = iter.next();
system.out.println(field + ":" + maps.get(field));
}
} catch (jsonparseexception e) {
e.printstacktrace();
} catch (jsonmappingexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
readjson2map(json);

以上内容是小编给大家介绍的java代码实现map和object互转及map和json的互转的相关知识,希望对大家有所帮助,如果大家想了解更多资讯敬请关注网站,谢谢!