DataConversionTool.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.steerinfo.route.util;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.ParameterizedType;
  7. import java.lang.reflect.Type;
  8. import java.math.BigDecimal;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. import java.util.logging.SimpleFormatter;
  12. //@Auther Tiroble
  13. //@meil 2439003195@qq.com
  14. public class DataConversionTool {
  15. /**
  16. *
  17. * @param json
  18. * @param type
  19. * @return
  20. * @throws Exception
  21. */
  22. public static Object jsonToBean(Object json, Object type) throws Exception {
  23. try{
  24. JSONObject jsonObject=null;
  25. //首先需要判断是否是json字符串如果是解析为jsonObject如果是JsonObject就直接赋值给jsonObject
  26. if (json instanceof String){
  27. //将json转为JSONOject
  28. jsonObject= JSONObject.parseObject(json.toString());
  29. }
  30. else {
  31. jsonObject= (JSONObject) json;
  32. }
  33. //获得Class对象
  34. //Class clazz=type.getClass().getDeclaringClass();
  35. Class clazz= (Class) type;
  36. //通过空参构造器创建实例
  37. Constructor constructor = clazz.getDeclaredConstructor();
  38. constructor.setAccessible(true);
  39. Object classObject = constructor.newInstance();
  40. //获得对象的所有属性名
  41. Field[] fields = clazz.getDeclaredFields();
  42. //遍历属性集合
  43. for (int i=0;i<fields.length;i++){
  44. Field field=fields[i];
  45. //开启权限
  46. field.setAccessible(true);
  47. //判断是否保护属性值
  48. if(jsonObject.containsKey(field.getName())){
  49. Object objectValue =jsonObject.get(field.getName());
  50. //判断jsonObject的item是否是String或者Integer,是简单类型直接赋值
  51. if ((objectValue instanceof Long)||(objectValue instanceof String)||(objectValue instanceof Integer)||(objectValue instanceof Boolean)||(objectValue instanceof BigDecimal)){
  52. //进行数据判断
  53. if (field.getType()==BigDecimal.class){
  54. field.set(classObject,new BigDecimal(objectValue.toString()));
  55. } else if (field.getType()==Short.class){
  56. field.set(classObject,Short.parseShort(objectValue.toString()));
  57. }else if (field.getType()==Date.class){
  58. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  59. Date time = formatter.parse(objectValue.toString());
  60. field.set(classObject,time);
  61. }
  62. else {
  63. if(field.getType()==Long.class||field.getType()==String.class||field.getType()==Integer.class||field.getType()==Boolean.class){
  64. field.set(classObject,objectValue);
  65. }
  66. }
  67. }
  68. //集合类型类型
  69. else if(objectValue instanceof JSONArray){
  70. Iterator<Object> iterator = ((JSONArray) objectValue).iterator();
  71. //这里使用的是arryList接收
  72. List list=new ArrayList<>();
  73. // 如果是List类型,得到其Generic的类型
  74. Type genericType = field.getGenericType();
  75. //如果是空的
  76. if(genericType == null) {
  77. genericType=Object.class;
  78. }
  79. // 如果是泛型参数的类型
  80. else if(genericType instanceof ParameterizedType){
  81. ParameterizedType pt = (ParameterizedType) genericType;
  82. //得到泛型里的class类型对象
  83. Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
  84. genericType=genericClazz;
  85. }
  86. while (iterator.hasNext()){
  87. Object nextObject = iterator.next();
  88. Object fieldValue = jsonToBean(nextObject, genericType);
  89. list.add(fieldValue);
  90. }
  91. field.set(classObject,list);
  92. }
  93. // else if(field.getType()==Object.class){
  94. // field.set(classObject,objectValue);
  95. // }
  96. //如果不是再判断是否是JSONOArray,复杂数据类型
  97. else{
  98. //如果是JSONObject需要判断是否是引用类型,如果是引用类型就还需要将值转为对应类型
  99. Object fieldValue = jsonToBean(objectValue, field.getType());
  100. field.set(classObject,fieldValue);
  101. }
  102. }
  103. }
  104. return classObject;
  105. }catch (Exception ex){
  106. throw ex;
  107. }
  108. }
  109. /**
  110. * 将Object对象里面的属性和值转化成Map对象
  111. *
  112. * @param obj
  113. * @return
  114. * @throws IllegalAccessException
  115. */
  116. public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
  117. Map<String, Object> map = new HashMap<String,Object>();
  118. Class<?> clazz = obj.getClass();
  119. for (Field field : clazz.getDeclaredFields()) {
  120. field.setAccessible(true);
  121. String fieldName = field.getName();
  122. Object value = field.get(obj);
  123. map.put(fieldName, value);
  124. }
  125. return map;
  126. }
  127. /**
  128. *
  129. * @param map
  130. * @param type
  131. * @return
  132. * @throws Exception
  133. */
  134. public static Object mapToBean(Map map, Object type) throws Exception {
  135. try{
  136. //获得Class对象
  137. //Class clazz=type.getClass().getDeclaringClass();
  138. Class clazz= (Class) type;
  139. //通过空参构造器创建实例
  140. Constructor constructor = clazz.getDeclaredConstructor();
  141. constructor.setAccessible(true);
  142. Object classObject = constructor.newInstance();
  143. //获得对象的所有属性名
  144. Field[] fields = clazz.getDeclaredFields();
  145. //遍历属性集合
  146. for (int i=0;i<fields.length;i++){
  147. Field field=fields[i];
  148. //开启权限
  149. field.setAccessible(true);
  150. //判断是否保护属性值
  151. if(map.containsKey(field.getName())){
  152. Object objectValue =map.get(field.getName());
  153. //判断jsonObject的item是否是String或者Integer,是简单类型直接赋值
  154. if ((objectValue instanceof String)||(objectValue instanceof Integer)||(objectValue instanceof Boolean)){
  155. //进行数据判断
  156. if (field.getType()==BigDecimal.class){
  157. field.set(classObject,new BigDecimal(objectValue.toString()));
  158. } else if (field.getType()==Short.class){
  159. if(objectValue.toString()=="0.00"){
  160. field.set(classObject,0);
  161. }
  162. field.set(classObject,Short.parseShort(objectValue.toString()));
  163. }else if (field.getType()==Date.class){
  164. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  165. Date time = formatter.parse(objectValue.toString());
  166. field.set(classObject,time);
  167. }
  168. else {
  169. field.set(classObject,objectValue);
  170. }
  171. }
  172. //集合类型类型
  173. else if(objectValue instanceof List){
  174. Iterator<Object> iterator = ((List)objectValue).iterator();
  175. //这里使用的是arryList接收
  176. List list=new ArrayList<>();
  177. // 如果是List类型,得到其Generic的类型
  178. Type genericType = field.getGenericType();
  179. //如果是空的
  180. if(genericType == null) {
  181. genericType=Object.class;
  182. }
  183. // 如果是泛型参数的类型
  184. else if(genericType instanceof ParameterizedType){
  185. ParameterizedType pt = (ParameterizedType) genericType;
  186. //得到泛型里的class类型对象
  187. Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
  188. genericType=genericClazz;
  189. }
  190. while (iterator.hasNext()){
  191. Object nextObject = iterator.next();
  192. Object fieldValue = jsonToBean(nextObject, genericType);
  193. list.add(fieldValue);
  194. }
  195. field.set(classObject,list);
  196. }
  197. //如果不是再判断是否是JSONOArray,复杂数据类型
  198. else{
  199. //如果是JSONObject需要判断是否是引用类型,如果是引用类型就还需要将值转为对应类型
  200. Object fieldValue = jsonToBean(objectValue, field.getType());
  201. field.set(classObject,fieldValue);
  202. }
  203. }
  204. }
  205. return classObject;
  206. }catch (Exception ex){
  207. throw ex;
  208. }
  209. }
  210. }