先给大家介绍下 Carte 和 Objet 互相转换的代码。
:
/ ** * 使用 org.apache.commons.beanutils 进行转换 * / class a {public static objet MaptoObject (map <string, objet> map, class <?> Beanclass) lève exception {if (map == null) return null; Objet obj = beanclass.newinstance (); org.apache.commons.beanutils.beanutils.populer (obj, map); retour obj; } Map statique publique <?,?> objectTomap (objet obj) {if (obj == null) return null; retourner new org.apache.commons.beanutils.beanmap (obj); }} / ** * 使用 Introspector 进行转换 * / class b {public static objet MaptoObject (map <string, objet> map, class <?> Beanclass) lance l'exception {if (map == null) return null; Objet obj = beanclass.newinstance (); Beaninfo beaninfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); pour (propriété Descriptor Property: PropertyDescriptors) {Method seter = propriété.getWriteMethod (); if (seter! = null) {seter.invoke (obj, map.get (propriété.getName ())); }} return obj; } public static map <string, object> objectToMap (objet obj) lève une exception {if (obj == null) return null; Map <string, object> map = new hashmap <string, object> (); Beaninfo beaninfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); pour (propriété Descriptor Property: PropertyDescriptors) {String key = propriété.getName (); if (key.comparetoignorecase ("class") == 0) {continuant; } Méthode getter = propriété.getReadMethod (); Valeur d'objet = Getter! = NULL? getter.invoke (obj): null; map.put (clé, valeur); } retour de la carte; }} / ** * 使用 Réfléchir 进行转换 * / class C {public static objet MaptoObject (map <string, objet> map, class <?> Beanclass) lève exception {if (map == null) return null; Objet obj = beanclass.newinstance (); Champ [] champs = obj.getClass (). GetDeclaredFields (); pour (champ de champ: champs) {int mod = field.getModifiers (); if (modificier.isstatic (mod) || modificateur.isfinal (mod)) {continue; } field.setAccessible (true); field.set (obj, map.get (field.getName ())); } return obj; } public static map <string, object> objectToMap (objet obj) lève une exception {if (obj == null) {return null; } Map <string, objet> map = new hashmap <string, object> (); Field [] DÉCLARDFIELDS = obj.getClass (). GetDeclaredFields (); pour (champ de champ: DarcladFields) {field.setAccessible (true); map.put (field.getName (), field.get (obj)); } retour de la carte; } <p>} </p> <p> </p> <p> de : http: //www.open-open.com/code/view/1423280939826 </p>下面给大家介绍 Carte 和 JSON 的互相转换
第一段代码
Map <string, objet> map = new hashmap <string, object> (); map.put ("méthode", "json"); map.put ("param", null); map.put ("time", "2015-01-23 10:54:55"); ObjectMapper Mapper = new ObjectMapper (); maaker.writeEaString (map);第二段代码
public static void Readjson2Map (String JSON) {objectMapper objectMapper = new ObjectMapper (); try {// 将 JSON 字符串转成 Map 结合解析出来 , 并打印 并打印 (这里以解析成 map 为例) map <string, map <string, object >> maps = objectMapper.readvalue (json, map.class); System.out.println (maps.size (); Maps.KeySet (); iterator <string> iter = key.iterator (); while (iter.hasnext ()) {string field = iter.next (); System.out.println (champ + ":" + maps.get (champ));}} catch (jsonparseException e) {e.printStackTrace ();} capture (jsonphe gens以上内容是小编给大家介绍的 Java 代码实现 Carte 和 Objet 互转及 Carte 和 JSON 的互转的相关知识 , 希望对大家有所帮助 , 如果大家想了解更多资讯敬请关注武林网网站 , 谢谢!