先给大家介绍下 Карта 和 объект 互相转换的代码。
具体代码如所示 :
/ ** * 使用 org.apache.commons.beanutils 进行转换 */ class a {public static Object maptoobject (map <string, object> map, class <?> Beanclass) бросает исключение {if (map = null) return null; Объект obj = beanclass.newinstance (); org.apache.commons.beanutils.beanutils.populate (obj, map); вернуть OBJ; } public Static Map <?,?> ObjectTomap (Object obj) {if (obj == null) return null; вернуть новый org.apache.commons.beanutils.beanmap (obj); }} / ** * 使用 Interspector 进行转换 * / Class B {public Static Object maptoobject (map <string> map, map, class <?> Beanclass) выбрасывает исключение {if (map = null) return null; Объект obj = beanclass.newinstance (); Beaninfo beaninfo = Interospepper.getbeaninfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); Для (свойства opportDescriptor: PropertyDescriptors) {метод setter = Property.getWriteMethod (); if (setter! = null) {setter.invoke (obj, map.get (property.getName ())); }} вернуть obj; } public Static Map <String, Object> ObjectTomap (Object obj) бросает исключение {if (obj == null) return null; Map <string, object> map = new hashmap <string, object> (); Beaninfo beaninfo = Interospepper.getbeaninfo (obj.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); для (свойства opportDescriptor: PropertyDescriptors) {String Key = Property.getName (); if (key.comparetoignorecase ("class") == 0) {продолжение; } Метод getter = Property.getReadMethod (); Значение объекта = getter! = Null? getter.invoke (obj): null; map.put (ключ, значение); } return Map; }} / ** * 使用 отражать 进行转换 * / class c {public Static Object maptoobject (map <string> map, class <?> Beanclass) Throws Exception {if (map = null) return null; Объект obj = beanclass.newinstance (); Field [] fields = obj.getClass (). GetDeclaredFields (); для (поле Поля: Fields) {int mod = field.getModifiers (); if (modifier.isstatic (mod) || modifier.isfinal (mod)) {продолжение; } field.SetAccessible (true); field.set (obj, map.get (field.getName ())); } вернуть obj; } public Static Map <String, Object> ObjectTomap (Object obj) бросает исключение {if (obj == null) {return null; } Map <string, object> map = new hashmap <string, object> (); Field [] decledfields = obj.getClass (). GetDeclaredFields (); для (поле Поля: DecledFields) {field.SetAccessible (true); map.put (field.getName (), field.get (obj)); } return Map; } <p>} </p> <p> </p> <p> из : http: //www.open-open.com/code/view/1423280939826 </p>下面给大家介绍 Карта 和 json 的互相转换
第一段代码
Map <string, object> map = new hashmap <string, object> (); map.put ("method", "json"); map.put ("param", null); map.put ("time", "2015-01-23 10:54:55"); objectmapper mapper = new objectmapper (); mapper.第二段代码
public static void readjson2map (string json) {objectmapper objectmapper = new objectmapper (); try {// 将 json 字符串转成 map 结合解析出来 , 并打印 (这里以解析成 map 为例) map <string, map, 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 (field));}} catch (jsonparseexception e) {e.printstacktrace (); {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}} readjson2map (json);以上内容是小编给大家介绍的 java 代码实现 карта 和 объект 互转及 карта 和 json 的互转的相关知识 希望对大家有所帮助 希望对大家有所帮助 , 如果大家想了解更多资讯敬请关注武林网网站 谢谢! 谢谢!