在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java代码实现Map和Object互转及Map和Json互转

2016-7-29 15:36| 发布者: zhangjf| 查看: 730| 评论: 0

摘要: 先给大家介绍下map和object互相转换的代码。 具体代码如所示: /** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(MapString, Object map, Class? beanClass) throws ...

先给大家介绍下map和object互相转换的代码。

具体代码如所示:

  1. /**
  2. * 使用org.apache.commons.beanutils进行转换
  3. */
  4. class A {
  5. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  6. if (map == null)
  7. return null;
  8. Object obj = beanClass.newInstance();
  9. org.apache.commons.beanutils.BeanUtils.populate(obj, map);
  10. return obj;
  11. }
  12. public static Map<?, ?> objectToMap(Object obj) {
  13. if(obj == null)
  14. return null;
  15. return new org.apache.commons.beanutils.BeanMap(obj);
  16. }
  17. }
  18. /**
  19. * 使用Introspector进行转换
  20. */
  21. class B {
  22. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  23. if (map == null)
  24. return null;
  25. Object obj = beanClass.newInstance();
  26. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  27. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  28. for (PropertyDescriptor property : propertyDescriptors) {
  29. Method setter = property.getWriteMethod();
  30. if (setter != null) {
  31. setter.invoke(obj, map.get(property.getName()));
  32. }
  33. }
  34. return obj;
  35. }
  36. public static Map<String, Object> objectToMap(Object obj) throws Exception {
  37. if(obj == null)
  38. return null;
  39. Map<String, Object> map = new HashMap<String, Object>();
  40. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  41. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  42. for (PropertyDescriptor property : propertyDescriptors) {
  43. String key = property.getName();
  44. if (key.compareToIgnoreCase("class") == 0) {
  45. continue;
  46. }
  47. Method getter = property.getReadMethod();
  48. Object value = getter!=null ? getter.invoke(obj) : null;
  49. map.put(key, value);
  50. }
  51. return map;
  52. }
  53. }
  54. /**
  55. * 使用reflect进行转换
  56. */
  57. class C {
  58. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  59. if (map == null)
  60. return null;
  61. Object obj = beanClass.newInstance();
  62. Field[] fields = obj.getClass().getDeclaredFields();
  63. for (Field field : fields) {
  64. int mod = field.getModifiers();
  65. if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
  66. continue;
  67. }
  68. field.setAccessible(true);
  69. field.set(obj, map.get(field.getName()));
  70. }
  71. return obj;
  72. }
  73. public static Map<String, Object> objectToMap(Object obj) throws Exception {
  74. if(obj == null){
  75. return null;
  76. }
  77. Map<String, Object> map = new HashMap<String, Object>();
  78. Field[] declaredFields = obj.getClass().getDeclaredFields();
  79. for (Field field : declaredFields) {
  80. field.setAccessible(true);
  81. map.put(field.getName(), field.get(obj));
  82. }
  83. return map;
  84. }
  85. <p>} </p><p>
  86. </p><p>from:http://www.open-open.com/code/view/1423280939826</p>
复制代码

下面给大家介绍Map和json的互相转换

第一段代码

  1. Map<String,Object> map = new HashMap<String,Object>();
  2. map.put("method","json");
  3. map.put("param",null);
  4. map.put("time","2015-01-23 10:54:55");
  5. ObjectMapper mapper = new ObjectMapper();
  6. mapper.writeValueAsString(map);
复制代码

第二段代码

  1. public static void readJson2Map(String json) {
  2. ObjectMapper objectMapper = new ObjectMapper();
  3. try {
  4. //将json字符串转成map结合解析出来,并打印(这里以解析成map为例)
  5. Map<String, Map<String, Object>> maps = objectMapper.readValue(
  6. json, Map.class);
  7. System.out.println(maps.size());
  8. Set<String> key = maps.keySet();
  9. Iterator<String> iter = key.iterator();
  10. while (iter.hasNext()) {
  11. String field = iter.next();
  12. System.out.println(field + ":" + maps.get(field));
  13. }
  14. } catch (JsonParseException e) {
  15. e.printStackTrace();
  16. } catch (JsonMappingException e) {
  17. e.printStackTrace();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. readJson2Map(json);
复制代码

以上内容是小编给大家介绍的java代码实现map和Object互转及Map和json的互转的相关知识,希望对大家有所帮助,如果大家想了解更多资讯敬请关注程序员之家网站,谢谢!

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-6 15:36

Copyright 2015-2025 djqfx

返回顶部