Before looking for such a method, found are not recursive implementation, the list, map inside did not turn, on their own toss A. --yoyo
PublicClassobjecttomap{PublicStaticMap objecttomap (Object obj) {Try{Class type =Obj.getclass (); Map Returnmap =NewHashMap (); BeanInfo BeanInfo =Introspector.getbeaninfo (type); propertydescriptor[] PropertyDescriptors =Beaninfo.getpropertydescriptors ();for (int i = 0; i< propertydescriptors.length; i++) {PropertyDescriptor descriptor =Propertydescriptors[i]; String propertyname =Descriptor.getname ();if (!propertyname.equals ("Class") {Method Readmethod =Descriptor.getreadmethod (); Object result = Readmethod.invoke (obj,New Object[0]);if (result = =Null){Continue; }//Determine if the underlying type string,boolean,byte,short,integer,long,float,double//Determine whether the collection class, Collection,mapif (ResultinstanceofString | | ResultinstanceofBoolean | | ResultinstanceofByte | | ResultinstanceofShort | | ResultinstanceofInteger | | ResultinstanceofLong | | ResultinstanceofFloat | | ResultinstanceofDouble | | ResultinstanceofEnum) {if (Result! =Null{Returnmap.put (propertyname, result);}}Elseif (ResultinstanceofCollection) {collection<?> lstobj = Arraytomap ((collection<?>) result); Returnmap.put (PropertyName, Lstobj); }Elseif (ResultinstanceofMAP) {map<object,object> lstobj = Maptomap ((map<object,object>) result); Returnmap.put (PropertyName, Lstobj); }Else{Map Mapresult =Objecttomap (result); Returnmap.put (PropertyName, Mapresult); } } }ReturnReturnmap; }Catch(Exception e) {ThrowNewRuntimeException (e); } }PrivateStatic Map<object, object> Maptomap (Map<object, object>Orignmap) {map<object,object> Resultmap =New hashmap<object,object>();For (Entry<object, object>Entry:orignMap.entrySet ()) {Object key =Entry.getkey (); Object Resultkey =Null;if (keyinstanceofCollection) {Resultkey =Arraytomap ((Collection) key); }Elseif (keyinstanceofMAP) {Resultkey =Maptomap ((MAP) key); }Else{if (keyinstanceofString | | KeyinstanceofBoolean | | KeyinstanceofByte | | KeyinstanceofShort | | KeyinstanceofInteger | | KeyinstanceofLong | | KeyinstanceofFloat | | KeyinstanceofDouble | | KeyinstanceofEnum) {if (Key! =Null) {Resultkey =Key } }Else{Resultkey =Objecttomap (key); }} Object value =Entry.getvalue (); Object Resultvalue =Null;if (valueinstanceofCollection) {Resultvalue =Arraytomap ((Collection) value); }Elseif (valueinstanceofMAP) {Resultvalue =Maptomap ((Map) value); }Else{if (valueinstanceofString | | ValueinstanceofBoolean | | ValueinstanceofByte | | ValueinstanceofShort | | ValueinstanceofInteger | | ValueinstanceofLong | | ValueinstanceofFloat | | ValueinstanceofDouble | | ValueinstanceofEnum) {if (Value! =Null) {Resultvalue =Value } }Else{Resultvalue =Objecttomap (value); }} resultmap.put (Resultkey, Resultvalue); }ReturnResultmap; }PrivateStaticCollection Arraytomap (Collection lstobj) {ArrayList ArrayList =NewArrayList ();For(Object t:lstobj) {if (tinstanceofCollection) {Collection result =Arraytomap ((Collection) t); Arraylist.add (result); }Elseif (tinstanceofMAP) {Map result =Maptomap ((MAP) t); Arraylist.add (result); }Else{if (tinstanceofString | | TinstanceofBoolean | | TinstanceofByte | | Tinstanceof short | | t Integer | | t instanceof Long | | T instanceof Float | | t Double | | t instanceof Enum) {if (t! = nullelse{Object result =return ArrayList;}}
JavaBean recursive Map implementation