Home > Java > javaTutorial > Java code realizes mutual conversion between Map and Object and between Map and Json

Java code realizes mutual conversion between Map and Object and between Map and Json

高洛峰
Release: 2017-01-17 16:38:37
Original
1388 people have browsed it

Let me first introduce to you the code for converting map and object to each other.

The specific code is as shown:

/**
* 使用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>
Copy after login

The following will introduce the mutual conversion between Map and json

The first paragraph Code

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);
Copy after login

The second code

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);
Copy after login

The above content is the Java code implementation map introduced by the editor I hope it will be helpful to you for the knowledge about the mutual conversion between Object and Map and json. If you want to know more information, please pay attention to the PHP Chinese website. Thank you!

For more Java code to implement Map and Object mutual conversion and Map and Json mutual conversion related articles, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template