欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-06-02 08:21:08|栏目:JAVA代码|点击:

先给大家介绍下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);

上一篇:使用WebSocket实现即时通讯(一个群聊的聊天室)

栏    目:JAVA代码

下一篇:Java中终止线程的方法详解

本文标题:Java代码实现Map和Object互转及Map和Json互转

本文地址:http://www.codeinn.net/misctech/133656.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有