First we introduce the next map and object conversion code.
The specific code looks like this:
/** * Use org.apache.commons.beanutils for conversion/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); }/** * Use Introspector for conversion/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; }/** * using reflect for conversion/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 >
Here's a brief introduction to the transformation of map and JSON
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);
Second paragraph code
public static void Readjson2map (String json) {
Objectmapper objectmapper = new Objectmapper ();
try {
///Convert the JSON string into a map to parse it out and print (here, for example, parse to 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);
The above is a small series to introduce the Java code to realize the map and object and map and JSON of the mutual transfer of knowledge, hope to help everyone, if you want to learn more information please pay attention to cloud Habitat community website, thank you!