|
@@ -0,0 +1,205 @@
|
|
|
+package com.steerinfo.dil.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author zhangnan
|
|
|
+ * @Date 2021/5/26 8:59
|
|
|
+ * @Version 1.0
|
|
|
+ * 工具类
|
|
|
+ */
|
|
|
+public class Util {
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param map
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Object mapToBean(Map map, Object type) throws Exception {
|
|
|
+
|
|
|
+ try{
|
|
|
+ //获得Class对象
|
|
|
+ //Class clazz=type.getClass().getDeclaringClass();
|
|
|
+ Class clazz= (Class) type;
|
|
|
+ //通过空参构造器创建实例
|
|
|
+ Constructor constructor = clazz.getDeclaredConstructor();
|
|
|
+ constructor.setAccessible(true);
|
|
|
+ Object classObject = constructor.newInstance();
|
|
|
+ //获得对象的所有属性名
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+ //遍历属性集合
|
|
|
+ for (int i=0;i<fields.length;i++){
|
|
|
+ Field field=fields[i];
|
|
|
+ //开启权限
|
|
|
+ field.setAccessible(true);
|
|
|
+ //判断是否保护属性值
|
|
|
+ if(map.containsKey(field.getName())){
|
|
|
+ Object objectValue =map.get(field.getName());
|
|
|
+ //判断jsonObject的item是否是String或者Integer,是简单类型直接赋值
|
|
|
+ if ((objectValue instanceof String)||(objectValue instanceof Integer)||(objectValue instanceof Boolean)){
|
|
|
+ //进行数据判断
|
|
|
+ if (field.getType()== BigDecimal.class){
|
|
|
+ field.set(classObject,new BigDecimal(objectValue.toString()));
|
|
|
+ } else if (field.getType()==Short.class){
|
|
|
+ if(objectValue.toString()=="0.00"){
|
|
|
+ field.set(classObject,0);
|
|
|
+ }
|
|
|
+ field.set(classObject,Short.parseShort(objectValue.toString()));
|
|
|
+ }else if (field.getType()== Date.class){
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date time = formatter.parse(objectValue.toString());
|
|
|
+ field.set(classObject,time);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ field.set(classObject,objectValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //集合类型类型
|
|
|
+ else if(objectValue instanceof List){
|
|
|
+ Iterator<Object> iterator = ((List)objectValue).iterator();
|
|
|
+ //这里使用的是arryList接收
|
|
|
+ List list=new ArrayList<>();
|
|
|
+ // 如果是List类型,得到其Generic的类型
|
|
|
+ Type genericType = field.getGenericType();
|
|
|
+ //如果是空的
|
|
|
+ if(genericType == null) {
|
|
|
+ genericType=Object.class;
|
|
|
+ }
|
|
|
+ // 如果是泛型参数的类型
|
|
|
+ else if(genericType instanceof ParameterizedType){
|
|
|
+ ParameterizedType pt = (ParameterizedType) genericType;
|
|
|
+ //得到泛型里的class类型对象
|
|
|
+ Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
|
|
|
+ genericType=genericClazz;
|
|
|
+ }
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Object nextObject = iterator.next();
|
|
|
+ Object fieldValue = jsonToBean(nextObject, genericType);
|
|
|
+ list.add(fieldValue);
|
|
|
+ }
|
|
|
+ field.set(classObject,list);
|
|
|
+ }
|
|
|
+ //如果不是再判断是否是JSONOArray,复杂数据类型
|
|
|
+ else{
|
|
|
+ //如果是JSONObject需要判断是否是引用类型,如果是引用类型就还需要将值转为对应类型
|
|
|
+ Object fieldValue = jsonToBean(objectValue, field.getType());
|
|
|
+ field.set(classObject,fieldValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return classObject;
|
|
|
+ }catch (Exception ex){
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param json
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Object jsonToBean(Object json, Object type) throws Exception {
|
|
|
+ try{
|
|
|
+ JSONObject jsonObject=null;
|
|
|
+ //首先需要判断是否是json字符串如果是解析为jsonObject如果是JsonObject就直接赋值给jsonObject
|
|
|
+ if (json instanceof String){
|
|
|
+ //将json转为JSONOject
|
|
|
+ jsonObject= JSONObject.parseObject(json.toString());
|
|
|
+ }else {
|
|
|
+ jsonObject= (JSONObject) json;
|
|
|
+ }
|
|
|
+ //获得Class对象
|
|
|
+ //Class clazz=type.getClass().getDeclaringClass();
|
|
|
+ Class clazz= (Class) type;
|
|
|
+ //通过空参构造器创建实例
|
|
|
+ Constructor constructor = clazz.getDeclaredConstructor();
|
|
|
+ constructor.setAccessible(true);
|
|
|
+ Object classObject = constructor.newInstance();
|
|
|
+ //获得对象的所有属性名
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+ //遍历属性集合
|
|
|
+ for (int i=0;i<fields.length;i++){
|
|
|
+ Field field=fields[i];
|
|
|
+ //开启权限
|
|
|
+ field.setAccessible(true);
|
|
|
+ //判断是否保护属性值
|
|
|
+ if(jsonObject.containsKey(field.getName())){
|
|
|
+ Object objectValue =jsonObject.get(field.getName());
|
|
|
+ //判断jsonObject的item是否是String或者Integer,是简单类型直接赋值
|
|
|
+ if ((objectValue instanceof Long)||(objectValue instanceof String)||(objectValue instanceof Integer)||(objectValue instanceof Boolean)||(objectValue instanceof BigDecimal)){
|
|
|
+ //进行数据判断
|
|
|
+ if (field.getType()==BigDecimal.class){
|
|
|
+ field.set(classObject,new BigDecimal(objectValue.toString()));
|
|
|
+ } else if (field.getType()==Short.class){
|
|
|
+ field.set(classObject,Short.parseShort(objectValue.toString()));
|
|
|
+ }else if (field.getType()==Date.class){
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date time = formatter.parse(objectValue.toString());
|
|
|
+ field.set(classObject,time);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ field.set(classObject,objectValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //集合类型类型
|
|
|
+ else if(objectValue instanceof JSONArray){
|
|
|
+ Iterator<Object> iterator = ((JSONArray) objectValue).iterator();
|
|
|
+ //这里使用的是arryList接收
|
|
|
+ List list=new ArrayList<>();
|
|
|
+ // 如果是List类型,得到其Generic的类型
|
|
|
+ Type genericType = field.getGenericType();
|
|
|
+ //如果是空的
|
|
|
+ if(genericType == null) {
|
|
|
+ genericType=Object.class;
|
|
|
+ }
|
|
|
+ // 如果是泛型参数的类型
|
|
|
+ else if(genericType instanceof ParameterizedType){
|
|
|
+ ParameterizedType pt = (ParameterizedType) genericType;
|
|
|
+ //得到泛型里的class类型对象
|
|
|
+ Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
|
|
|
+ genericType=genericClazz;
|
|
|
+ }
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Object nextObject = iterator.next();
|
|
|
+ Object fieldValue = jsonToBean(nextObject, genericType);
|
|
|
+ list.add(fieldValue);
|
|
|
+ }
|
|
|
+ field.set(classObject,list);
|
|
|
+ }
|
|
|
+ //如果不是再判断是否是JSONOArray,复杂数据类型
|
|
|
+ else{
|
|
|
+ //如果是JSONObject需要判断是否是引用类型,如果是引用类型就还需要将值转为对应类型
|
|
|
+ Object fieldValue = jsonToBean(objectValue, field.getType());
|
|
|
+ field.set(classObject,fieldValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return classObject;
|
|
|
+ }catch (Exception ex){
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|