先给大家介绍下 Karte 和 Objekt 互相转换的代码。
具体代码如所示 :
/ ** * 使用 org.apache.commons.beanUtils 进行转换 */ class A {public statisches Objekt MaptoObject (MAP <String, Objekt> Karte, Klasse <?> BeanClass) löst eine Ausnahme aus {if (map == null) return null; Objekt obj = BeanClass.Newinstance (); org.apache.commons.beanutils.beanutils.populate (obj, map); Rückkehr obj; } public static map <?,?> ObjectTomap (Objekt obj) {if (obj == null) return null; return New org.apache.commons.beanutils.BeanMap (OBJ); }} / ** * 使用 Introspector 进行转换 * / Klasse B {public statisches Objekt MaptoObject (Map <String, Objekt> Karte, Klasse <?> Beanclass) löst eine Ausnahme aus {if (map == null) return null; Objekt obj = BeanClass.Newinstance (); BeanInfo BeanInfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); für (PropertyDescriptor -Eigenschaft: PropertyDescriptors) {Methode Setter = Property.getWritemethod (); if (setter! }} return obj; } public static map <String, Object> ObjectTomap (Object OBJ) löst Ausnahme aus {if (obj == null) return null; Karte <String, Objekt> map = new HashMap <String, Object> (); BeanInfo BeanInfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); für (PropertyDescriptor -Eigenschaft: PropertyDescriptors) {String key = property.getName (); if (key.comParetoignoreCase ("Klasse") == 0) {Fortsetzung; } Method getter = property.getreadMethod (); Objektwert = Getter! = NULL? Getter.invoke (obj): null; map.put (Schlüssel, Wert); } Rückgabekarte; }} / ** * 使用 reflektieren 进行转换 * / Klasse c {public static Object maptoObject (map <String, Objekt> Karte, Klasse <?> BeanClass) löst eine Ausnahme aus {if (map == null) return null; Objekt obj = BeanClass.Newinstance (); Field [] fields = obj.getClass (). GetDeclaredfields (); für (Feldfeld: Felder) {int mod = field.getModifiers (); if (modifier.isstatic (mod) || modifier.isfinal (mod)) {Fortsetzung; } field.setAccessible (true); field.set (obj, map.get (field.getName ())); } return obj; } public static map <String, Object> ObjectTomap (Object Obj) löst Ausnahme aus {if (obj == null) {return null; } Map <string, object> map = new HashMap <String, Object> (); Field [] deklaredFields = obj.getClass (). GetDeclaredfields (); für (Feldfeld: deklariertefields) {field.setAccessible (true); map.put (field.getName (), field.get (obj)); } Rückgabekarte; } <p>} </p> <p> </p> <p> Von : http: //www.open-open.com/code/view/1423280939826 </p>下面给大家介绍 Karte 和 json 的互相转换
第一段代码
Karte <String, Objekt> map = new HashMap <String, Object> (); map.put ("Methode", "json"); map.put ("param", null); map.put ("time", "2015-01-23 10:54:55"); ObjectMapper MAPPER = New ObjectMaper (); mapper.writeValuastring (mapPer).第二段代码
public static void readjson2map (String json) {ObjectMapper ObjectMapper = new ObjectMapper (); try {// 将 JSON 字符串转成 MAP 结合解析出来 , 并打印 (这里以解析成 map 为例) map <String, map <String, Objekt >> maps = ObjectMapper.ReadValue (json); maps.keyset (); iterator <string> iter = key.Iterator (); while (iter.hasnext ()) {String field = iter.next (); System.out.println (field + ":" + maps.get (field));}} catch (jsonParsexception E) {E. printace (); {e.printstacktrace ();} catch (ioException e) {e.printstacktrace ();}} Readjson2Map (json);以上内容是小编给大家介绍的 Java 代码实现 Karte 和 Objekt 互转及 Karte 和 json 的互转的相关知识 , 希望对大家有所帮助 , 如果大家想了解更多资讯敬请关注武林网网站 , 谢谢! 谢谢! 谢谢!