先给大家介绍下 맵 互相转换的代码。 객체 和
具体代码如所示 :
/ ** * 使用 org.apache.commons.beanutils 进行转换 */ class a {public static object maptoobject (map <string, object> map, class <?> beanclass) 예외 {if (map == null) return null; Object 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)을 반환합니다. }} / ** * 使用 introspector 进行转换 * / class b {public static 객체 maptoobject (map <string, object> map, class <?> beanclass) 예외 {if (map == null) return null; Object obj = beanclass.newinstance (); beaninfo beaninfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors (); for (propertyDescriptor 속성 : propertyDescriptors) {method setter = property.getWritemEthod (); if (setter! = null) {setter.invoke (obj, map.get (property.getname ())); }} return obj; } public static map <string, object> objectTomap (Object obj)는 예외를 던지려면 {if (obj == null) return null; map <string, object> map = new Hashmap <String, Object> (); beaninfo beaninfo = introspector.getBeanInfo (obj.getClass ()); PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors (); for (propertyDescriptor 속성 : propertyDescriptors) {String key = property.getName (); if (key.comparetoignorecase ( "class") == 0) {계속; } 메소드 getter = property.getReadMethod (); 개체 값 = getter! = null? getter.invoke (obj) : null; map.put (키, 값); } 리턴 맵; }}} / ** * 使用 반사 进行转换 * / class C {public static 객체 maptoobject (map <string, object> map, class <?> beanclass)는 예외 {if (map == null) return null; Object obj = beanclass.newinstance (); 필드 [] fields = obj.getClass (). getDeclaredFields (); for (필드 필드 : 필드) {int mod = field.getModifiers (); if (modifier.isstatic (mod) || modifier.isfinal (mod)) {계속; } field.setAccessible (true); field.set (obj, map.get (field.getName ())); } return obj; } public static map <string, object> objectTomap (Object obj)는 예외를 던지려면 {if (obj == null) {return null; } map <String, object> map = new Hashmap <String, Object> (); field [] declaredfields = obj.getClass (). getDeclaredFields (); for (필드 필드 : deLlearedfields) {field.setAccessible (true); map.put (field.getName (), field.get (obj)); } 리턴 맵; } <p>} </p> <p> </p> <p> from : http : //www.open-open.com/code/view/1423280939826 </p>MAP son 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.writeValeOeasString (MapPer.writeValeOeasString);
第二段代码
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); sets.println (maps.size (maps.size); maps.keyset (); iterator <string> iter = key.iterator (); while (iter.hasnext ()) {string field = iter.next (); system.out.println (field + ":" + maps.get (field));}} catch (jsonparsecection e) {e.printsctactrace (jsonmappencectectrace e); {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}} readjson2map (json);以上内容是小编给大家介绍的 java a MAP 和 객체 互转及 MAP 和 JSON 的互转的相关知识 的互转的相关知识, 希望对大家有所帮助, 如果大家想了解更多资讯敬请关注武林网网站, 谢谢! 谢谢!