先给大家介绍下 Mapa 和 Objeto 互相转换的代码。
具体代码如所示
/ ** * 使用 org.apache.commons.beanutils 进行转换 */ classe A {public static objeto maptoObject (map <string, object> map, classe <?> Beansclass) lança exceção {if (map == null) return null; Objeto obj = beansclass.newInstance (); org.apache.commons.beanutils.beanutils.populate (obj, mapa); retornar obj; } mapa estático público <?,?> objectTOMap (object obj) {if (obj == null) retorna nulo; retornar new org.apache.commons.beanutils.beanmap (obj); }} / ** * 使用 INTROSPECOR 进行转换 * / Classe B {Public Static Object MaptoObject (map <string, object> map, classe <?> Beansclass) lança exceção {if (map == null) return null; Objeto obj = beansclass.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 ())); }} retornar obj; } mapa estático público <string, object> objectTOMap (object obj) lança exceção {if (obj == null) return null; Mapa <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 ("classe") == 0) {continuação; } Método getter = Property.getReadMethod (); Valor do objeto = getter! = Nulo? getter.invoke (obj): null; map.put (chave, valor); } mapa de retorno; }} / ** * 使用 refletir 进行转换 * / classe C {public static objeto maptoObject (map <string, objeto> map, classe <?> Beansclass) lança a exceção {if (map == null) return null; Objeto obj = beansclass.newInstance (); Campo [] campos = obj.getclass (). GetDecLaredFields (); for (campo de campo: campos) {int mod = field.getModifiers (); if (modifier.isstatic (mod) || modifier.isfinal (mod)) {continuação; } field.setAccessible (true); field.set (obj, map.get (field.getName ())); } retornar obj; } mapa estático público <string, object> objectTOMap (object obj) lança exceção {if (obj == null) {return null; } Mapa <string, object> map = new hashmap <string, object> (); Campo [] declararfields = obj.getclass (). GetDecLaredFields (); for (campo de campo: declaradofields) {field.setAccessible (true); map.put (field.getName (), field.get (obj)); } mapa de retorno; } <p>} </p> <p> </p> <p> de: http: //www.open-open.com/code/view/1423280939826 </p>下面给大家介绍 Mapa 和 JSON 的互相转换
第一段代码
Map <string, object> map = new Hashmap <string, object> (); map.put ("método", "json"); map.put ("param", null); map.put ("time", "2015-01-23 10:54:55"); objecjormapper = new objecjotMapper (); mapper.weastring ");第二段代码
public static void readjson2Map (String json) {objectMapper ObjectMapper = new ObjectMapper (); Try {// 将 JSON 字符串转成 MAP 结合解析出来 , 并打印 (这里以解析成 MAP 为例) mapa <string, string <string, object >> maps = objectmapper.readValue (json, map.class); maps.KeySet (); iterator <string> iter = key.iterator (); while (iter.hasnext ()) {string field = iter.Next (); system.out.println (field + ":" + maps.get (campo));}} catch (jsonparseException e) (e.printack (field); {E.PrintStackTrace ();} catch (ioexception e) {e.printStackTrace ();}} readjson2Map (json);以上内容是小编给大家介绍的 java 代码实现 mapa 和 objeto 互转及 mapa 和 json 的互转的相关知识 , , , , , 谢谢!