1./** 2. * 从一个JSON 对象字符格式中得到一个java对象,形如: 3. * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}} 4. * @param object 5. * @param clazz 6. * @return 7. */ 8. public static Object getDTO(String jsonString, Class clazz){ 9. JSONObject jsonObject = null; 10. try{ 11. setDataFormat2JAVA(); 12. jsonObject = JSONObject.fromObject(jsonString); 13. }catch(Exception e){ 14. e.printStackTrace(); 15. } 16. return JSONObject.toBean(jsonObject, clazz); 17. } 18. 19. /** 20. * 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如: 21. * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}, 22. * beansList:[{}, {}, ...]} 23. * @param jsonString 24. * @param clazz 25. * @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class) 26. * @return 27. */ 28. public static Object getDTO(String jsonString, Class clazz, Map map){ 29. JSONObject jsonObject = null; 30. try{ 31. setDataFormat2JAVA(); 32. jsonObject = JSONObject.fromObject(jsonString); 33. }catch(Exception e){ 34. e.printStackTrace(); 35. } 36. return JSONObject.toBean(jsonObject, clazz, map); 37. } 38. 39. /** 40. * 从一个JSON数组得到一个java对象数组,形如: 41. * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...] 42. * @param object 43. * @param clazz 44. * @return 45. */ 46. public static Object[] getDTOArray(String jsonString, Class clazz){ 47. setDataFormat2JAVA(); 48. JSONArray array = JSONArray.fromObject(jsonString); 49. Object[] obj = new Object[array.size()]; 50. for(int i = 0; i < array.size(); i++){ 51. JSONObject jsonObject = array.getJSONObject(i); 52. obj[i] = JSONObject.toBean(jsonObject, clazz); 53. } 54. return obj; 55. } 56. 57. /** 58. * 从一个JSON数组得到一个java对象数组,形如: 59. * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...] 60. * @param object 61. * @param clazz 62. * @param map 63. * @return 64. */ 65. public static Object[] getDTOArray(String jsonString, Class clazz, Map map){ 66. setDataFormat2JAVA(); 67. JSONArray array = JSONArray.fromObject(jsonString); 68. Object[] obj = new Object[array.size()]; 69. for(int i = 0; i < array.size(); i++){ 70. JSONObject jsonObject = array.getJSONObject(i); 71. obj[i] = JSONObject.toBean(jsonObject, clazz, map); 72. } 73. return obj; 74. } 75. 76. /** 77. * 从一个JSON数组得到一个java对象集合 78. * @param object 79. * @param clazz 80. * @return 81. */ 82. public static List getDTOList(String jsonString, Class clazz){ 83. setDataFormat2JAVA(); 84. JSONArray array = JSONArray.fromObject(jsonString); 85. List list = new ArrayList(); 86. for(Iterator iter = array.iterator(); iter.hasNext();){ 87. JSONObject jsonObject = (JSONObject)iter.next(); 88. list.add(JSONObject.toBean(jsonObject, clazz)); 89. } 90. return list; 91. } 92. 93. /** 94. * 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性 95. * @param object 96. * @param clazz 97. * @param map 集合属性的类型 98. * @return 99. */ 100. public static List getDTOList(String jsonString, Class clazz, Map map){ 101. setDataFormat2JAVA(); 102. JSONArray array = JSONArray.fromObject(jsonString); 103. List list = new ArrayList(); 104. for(Iterator iter = array.iterator(); iter.hasNext();){ 105. JSONObject jsonObject = (JSONObject)iter.next(); 106. list.add(JSONObject.toBean(jsonObject, clazz, map)); 107. } 108. return list; 109. } 110. 111. /** 112. * 从json HASH表达式中获取一个map,该map支持嵌套功能 113. * 形如:{"id" : "johncon", "name" : "小强"} 114. * 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap 115. * @param object 116. * @return 117. */ 118. public static Map getMapFromJson(String jsonString) { 119. setDataFormat2JAVA(); 120. JSONObject jsonObject = JSONObject.fromObject(jsonString); 121. Map map = new HashMap(); 122. for(Iterator iter = jsonObject.keys(); iter.hasNext();){ 123. String key = (String)iter.next(); 124. map.put(key, jsonObject.get(key)); 125. } 126. return map; 127. } 128. 129. /** 130. * 从json数组中得到相应java数组 131. * json形如:["123", "456"] 132. * @param jsonString 133. * @return 134. */ 135. public static Object[] getObjectArrayFromJson(String jsonString) { 136. JSONArray jsonArray = JSONArray.fromObject(jsonString); 137. return jsonArray.toArray(); 138. } 139. 140. 141. /** 142. * 把数据对象转换成json字符串 143. * DTO对象形如:{"id" : idValue, "name" : nameValue, ...} 144. * 数组对象形如:[{}, {}, {}, ...] 145. * map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...} 146. * @param object 147. * @return 148. */ 149. public static String getJSONString(Object object) throws Exception{ 150. String jsonString = null; 151. //日期值处理器 152. JsonConfig jsonConfig = new JsonConfig(); 153. jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor()); 154. if(object != null){ 155. if(object instanceof Collection || object instanceof Object[]){ 156. jsonString = JSONArray.fromObject(object, jsonConfig).toString(); 157. }else{ 158. jsonString = JSONObject.fromObject(object, jsonConfig).toString(); 159. } 160. } 161. return jsonString == null ? "{}" : jsonString; 162. } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/madun/archive/2010/08/02/5782882.aspx 需要jar包:ezmorph-1.0.6.jar,json-lib-2.3.jar 创建java对象: public class Address { private String road; private String streate; private String provience; private String no; public String getRoad() { return road; } public void setRoad(String road) { this.road = road; } public String getStreate() { return streate; } public void setStreate(String streate) { this.streate = streate; } public String getProvience() { return provience; } public void setProvience(String provience) { this.provience = provience; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } } 1.将json对象转化为java对象 JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}"); Address Address = (Address) JSONObject.toBean(jsonObject,Address.class); log.info(Address.getNo()); log.info(Address.getStreate()); log.info(Address.getProvience()); log.info(Address.getRoad()); 2.将java对象转化为json对象 将java对象转化为json对象: Address address = new Address(); address.setNo("104"); address.setProvience("陕西"); address.setRoad("高新路"); address.setStreate(""); JSONArray json = JSONArray.fromObject(address); log.info(json.toString()); 将java对象list转化为json对象: Address address = new Address(); address.setNo("104"); address.setProvience("陕西"); address.setRoad("高新路"); address.setStreate(""); Address address2 = new Address(); address2.setNo("105"); address2.setProvience("陕西"); address2.setRoad("未央路"); address2.setStreate("张办"); List list = new ArrayList(); list.add(address); list.add(address2); JSONArray json = JSONArray.fromObject(list); log.info(json.toString()); 3.JSONArray转化为list JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}"); JSONArray jsonArray = new JSONArray(); jsonArray.add("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}"); jsonArray.add("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"123\"}"); Object object = JSONArray.toList(jsonArray,Address.class);