先给大家介绍下 Mapa 和 Objeto 互相转换的代码。
具体代码如所示 :
/ ** * 使用 org.apache.commons.beanutils 进行转换 */ class A {public static Object MaptoObject (map <string, object> map, class <?> Beanclass) lanza excepción {if (map == null) return null; Object obj = beanclass.newinstance (); org.apache.commons.beanutils.beanutils.popule (obj, map); regresar obj; } public static map <?,?> ObjectTomap (object obj) {if (obj == null) return null; devolver nuevo org.apache.commons.beanutils.beanMap (obj); }} / ** * 使用 Introspector 进行转换 * / clase B {objeto estático público MaptoObject (map <string, objeto> map, class <?> Beanclass) lanza la excepción {if (map == null) return null; Object obj = beanclass.newinstance (); Beaninfo beaninfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beaninfo.getPropertyDescriptors (); for (propiedad PropertyDescriptor: PropertyDescriptors) {método setter = property.getWriteMethod (); if (setter! = null) {setter.invoke (obj, map.get (propiedad.getName ())); }} return obj; } public static map <string, object> objectTomap (object obj) lanza la excepción {if (obj == null) return null; Map <string, object> map = new HashMap <String, Object> (); Beaninfo beaninfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beaninfo.getPropertyDescriptors (); for (propiedad PropertyDescriptor: PropertyDescriptors) {String Key = Property.getName (); if (key.Comparetoignorecase ("class") == 0) {continuar; } Método getter = property.getReadMethod (); Valor de objeto = Getter! = NULL? getter.invoke (obj): nulo; map.put (clave, valor); } mapa de retorno; }} / ** * 使用 reflejar 进行转换 * / class C {Public Static Object MaptoObject (map <string, object> map, class <?> BeanClass) lanza excepción {if (map == null) return null; Object obj = beanclass.newinstance (); Campo [] campos = obj.getClass (). GetDeclaredfields (); for (campo campo: campos) {int mod = field.getModifiers (); if (modificador.isstatic (mod) || modificador.isfinal (mod)) {continuar; } campo.setAccessible (verdadero); field.set (obj, map.get (field.getName ())); } return obj; } public static map <string, object> objectTomap (object obj) lanza la excepción {if (obj == null) {return null; } Map <string, object> map = new HashMap <String, Object> (); Campo [] declaradofields = obj.getClass (). GetDeclaredfields (); para (campo campo: declaradofields) {field.setAccessible (true); map.put (field.getName (), field.get (obj)); } mapa de retorno; } <p>} </p> <p> </p> <p> Desde: 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"); ObjectMapper Mapper = new ObjectMapper (); mapper.writeValueasstring (MAP);第二段代码
public static void readjson2map (string json) {ObjectMapper ObjectMapper = new ObjectMapper (); intento {// 将 JSON 字符串转成 MAP 结合解析出来 , 并打印 并打印 (这里以解析成 MAP 为例) MAP <String, Map <String, Object >> Maps = ObjectMapper.ReadValue (JSON, MAP.Class); System.out.Println (MAPS.SEATE (STARTHVALUE); 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 ();} Jsonmaping E) {E.PrintStackTrace ();} Catch (ioException e) {E.PrintStackTrace ();}} readJson2Map (JSON);以上内容是小编给大家介绍的 java 代码实现 mapa 和 objeto 互转及 mapa 和 json 的互转的相关知识 , 希望对大家有所帮助 , 如果大家想了解更多资讯敬请关注武林网网站 谢谢! 谢谢!