Java代码实现Map和Object互转及Map和Json互转
先给大家介绍下map和object互相转换的代码。
具体代码如所示:
/**
*使用org.apache.commons.beanutils进行转换
*/
classA{
publicstaticObjectmapToObject(Map<String,Object>map,Class<?>beanClass)throwsException{
if(map==null)
returnnull;
Objectobj=beanClass.newInstance();
org.apache.commons.beanutils.BeanUtils.populate(obj,map);
returnobj;
}
publicstaticMap<?,?>objectToMap(Objectobj){
if(obj==null)
returnnull;
returnneworg.apache.commons.beanutils.BeanMap(obj);
}
}
/**
*使用Introspector进行转换
*/
classB{
publicstaticObjectmapToObject(Map<String,Object>map,Class<?>beanClass)throwsException{
if(map==null)
returnnull;
Objectobj=beanClass.newInstance();
BeanInfobeanInfo=Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[]propertyDescriptors=beanInfo.getPropertyDescriptors();
for(PropertyDescriptorproperty:propertyDescriptors){
Methodsetter=property.getWriteMethod();
if(setter!=null){
setter.invoke(obj,map.get(property.getName()));
}
}
returnobj;
}
publicstaticMap<String,Object>objectToMap(Objectobj)throwsException{
if(obj==null)
returnnull;
Map<String,Object>map=newHashMap<String,Object>();
BeanInfobeanInfo=Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[]propertyDescriptors=beanInfo.getPropertyDescriptors();
for(PropertyDescriptorproperty:propertyDescriptors){
Stringkey=property.getName();
if(key.compareToIgnoreCase("class")==0){
continue;
}
Methodgetter=property.getReadMethod();
Objectvalue=getter!=null?getter.invoke(obj):null;
map.put(key,value);
}
returnmap;
}
}
/**
*使用reflect进行转换
*/
classC{
publicstaticObjectmapToObject(Map<String,Object>map,Class<?>beanClass)throwsException{
if(map==null)
returnnull;
Objectobj=beanClass.newInstance();
Field[]fields=obj.getClass().getDeclaredFields();
for(Fieldfield:fields){
intmod=field.getModifiers();
if(Modifier.isStatic(mod)||Modifier.isFinal(mod)){
continue;
}
field.setAccessible(true);
field.set(obj,map.get(field.getName()));
}
returnobj;
}
publicstaticMap<String,Object>objectToMap(Objectobj)throwsException{
if(obj==null){
returnnull;
}
Map<String,Object>map=newHashMap<String,Object>();
Field[]declaredFields=obj.getClass().getDeclaredFields();
for(Fieldfield:declaredFields){
field.setAccessible(true);
map.put(field.getName(),field.get(obj));
}
returnmap;
}
<p>}</p><p>
</p><p>from:http://www.open-open.com/code/view/1423280939826</p>
下面给大家介绍Map和json的互相转换
第一段代码
Map<String,Object>map=newHashMap<String,Object>();
map.put("method","json");
map.put("param",null);
map.put("time","2015-01-2310:54:55");
ObjectMappermapper=newObjectMapper();
mapper.writeValueAsString(map);
第二段代码
publicstaticvoidreadJson2Map(Stringjson){
ObjectMapperobjectMapper=newObjectMapper();
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()){
Stringfield=iter.next();
System.out.println(field+":"+maps.get(field));
}
}catch(JsonParseExceptione){
e.printStackTrace();
}catch(JsonMappingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
readJson2Map(json);
以上内容是小编给大家介绍的Java代码实现map和Object互转及Map和json的互转的相关知识,希望对大家有所帮助,如果大家想了解更多资讯敬请关注毛票票网站,谢谢!