java map转object

 public static<T> T mapToObject(Object object,Class<T> clazz){
        if(!(object instanceof Map)){
            return (T)object;
        }
        Map map = (Map)object;
        Set<Map.Entry> entrySet = map.entrySet();
        Iterator<Map.Entry> iterator = entrySet.iterator();
        Map.Entry entry;
        Method methods[] = clazz.getMethods();
        T dx = null;
        try {
            dx = clazz.newInstance();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        while (iterator.hasNext()){
            entry = iterator.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if(value==null){
                continue;
            }
            String sKey = (String) key;
            String methodName = "set"+sKey.substring(0,1).toUpperCase()+sKey.substring(1,sKey.length());
            ws:for(Method method:methods){
                if(method.getName().equals(methodName)){
                    Class c[] = method.getParameterTypes();
                    if(c.length==1){
                        if(c[0]==int.class||c[0]==Integer.class){
                            try {
                                method.invoke(dx,Integer.parseInt(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }


                        }
                        else if(c[0]==byte.class||c[0]==Byte.class){
                            try {
                                method.invoke(dx,Byte.parseByte(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==char.class||c[0]==Character.class){
                            try {
                                method.invoke(dx,value);
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==boolean.class||c[0]==Boolean.class){
                            try {
                                method.invoke(dx,Boolean.parseBoolean(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==short.class||c[0]==Short.class){
                            try {
                                method.invoke(dx,Short.parseShort(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==long.class||c[0]==Long.class){
                            try {
                                method.invoke(dx,Long.parseLong(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==float.class||c[0]==Float.class){
                            try {
                                method.invoke(dx,Float.parseFloat(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==double.class||c[0]==Double.class){
                            try {
                                method.invoke(dx,Double.parseDouble(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==String.class){
                            try {
                                method.invoke(dx,String.valueOf(value));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0]==BigInteger.class){
                            try {
                                method.invoke(dx,BigInteger.valueOf(Long.parseLong(String.valueOf(value))));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0]==BigDecimal.class){
                            try {
                                method.invoke(dx,new BigDecimal(String.valueOf(value)));
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0]==Date.class){
                            try {
                                method.invoke(dx,value);
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        //对于数组的转换
                        else if(c[0]==int[].class||c[0]==Integer[].class){
                            try {


                                if(c[0]==int[].class){
                                    int[] objects = (int[])value;
                                    int[] ints = new int[objects.length];
                                    for(int i = 0; i < ints.length; i++){
                                        ints[i] = Integer.parseInt(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)ints);
                                }
                                if(c[0]==Integer[].class){
                                    Integer[] objects = (Integer[])value;
                                    Integer[] ints = new Integer[objects.length];
                                    for(int i = 0; i < ints.length; i++){
                                        ints[i] = Integer.parseInt(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)ints);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }


                        }
                        else if(c[0]==byte[].class||c[0]==Byte[].class){
                            try {
                                if(c[0]==byte[].class){
                                    byte[] objects = (byte[])value;
                                    byte[] bytes = new byte[objects.length];
                                    for(int i = 0; i < bytes.length; i++){
                                        bytes[i] = Byte.parseByte(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)bytes);
                                }
                                if(c[0]==Byte[].class){
                                    Byte[] objects = (Byte[])value;
                                    Byte[] bytes = new Byte[objects.length];
                                    for(int i = 0; i < bytes.length; i++){
                                        bytes[i] = Byte.parseByte(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)bytes);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==char[].class||c[0]==Character[].class){
                            try {




                                if(c[0]==char[].class){
                                    char[] objects = (char[])value;
                                    char[] chars = new char[objects.length];
                                    for(int i = 0; i < chars.length; i++){
                                        chars[i] = (char)objects[i];
                                    }
                                    method.invoke(dx,(Object)chars);
                                }
                                if(c[0]==Character[].class){
                                    Character[] objects = (Character[])value;
                                    Character[] characters = new Character[objects.length];
                                    for(int i = 0; i < characters.length; i++){
                                        characters[i] = (Character)objects[i];
                                    }
                                    method.invoke(dx,(Object)characters);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==boolean[].class||c[0]==Boolean[].class){
                            try {
                                if(c[0]==boolean[].class){
                                    boolean[] objects = (boolean[])value;
                                    boolean[] booleans = new boolean[objects.length];
                                    for(int i = 0; i < booleans.length; i++){
                                        booleans[i] = Boolean.parseBoolean(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)booleans);
                                }
                                if(c[0]==Boolean[].class){
                                    Boolean[] objects = (Boolean[])value;
                                    Boolean[] booleans = new Boolean[objects.length];
                                    for(int i = 0; i < booleans.length; i++){
                                        booleans[i] = Boolean.parseBoolean(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)booleans);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==short[].class||c[0]==Short[].class){
                            try {




                                if(c[0]==short[].class){
                                    short[] objects = (short[])value;
                                    short[] shorts = new short[objects.length];
                                    for(int i = 0; i < shorts.length; i++){
                                        shorts[i] = Short.parseShort(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)shorts);
                                }
                                if(c[0]==Short[].class) {
                                    Short[] objects = (Short[])value;
                                    Short[] shorts = new Short[objects.length];
                                    for (int i = 0; i < shorts.length; i++) {
                                        shorts[i] = Short.parseShort(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx, (Object)shorts);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==long[].class||c[0]==Long[].class){
                            try {






                                if(c[0]==long[].class){
                                    long[] objects = (long[])value;
                                    long[] longs = new long[objects.length];
                                    for(int i = 0; i < longs.length; i++){
                                        longs[i] = Long.parseLong(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)longs);
                                }
                                if(c[0]==Long[].class) {
                                    Long[] objects = (Long[])value;
                                    Long[] longs = new Long[objects.length];
                                    for (int i = 0; i < longs.length; i++) {
                                        longs[i] = Long.parseLong(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx, (Object) longs);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==float[].class||c[0]==Float[].class){
                            try {
                                if(c[0]==float[].class){
                                    float[] objects = (float[])value;
                                    float[] floats = new float[objects.length];
                                    for(int i = 0; i < floats.length; i++){
                                        floats[i] = Float.parseFloat(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)floats);
                                }
                                if(c[0]==Float[].class) {
                                    Float[] objects = (Float[])value;
                                    Float[] floats = new Float[objects.length];
                                    for (int i = 0; i < floats.length; i++) {
                                        floats[i] = Float.parseFloat(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx, (Object)floats);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==double[].class||c[0]==Double[].class){
                            try {
                                if(c[0]==double[].class){
                                    double[] objects = (double[])value;
                                    double[] doubles = new double[objects.length];
                                    for(int i = 0; i < doubles.length; i++){
                                        doubles[i] = Double.parseDouble(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx,(Object)doubles);
                                }
                                if(c[0]==Double[].class) {
                                    Double[] objects = (Double[])value;
                                    Double[] doubles = new Double[objects.length];
                                    for (int i = 0; i < doubles.length; i++) {
                                        doubles[i] = Double.parseDouble(String.valueOf(objects[i]));
                                    }
                                    method.invoke(dx, (Object)doubles);
                                }
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        else if(c[0]==String[].class){
                            try {
                                Object[] objects = (Object[])value;
                                String[] strings = new String[objects.length];
                                for(int i = 0; i < strings.length; i++){
                                    strings[i] = String.valueOf(objects[i]);
                                }
                                method.invoke(dx,(Object)strings);
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0]==BigInteger[].class){
                            try {
                                Object[] objects = (Object[])value;
                                BigInteger[] bigIntegers = new BigInteger[objects.length];
                                for(int i = 0; i < bigIntegers.length; i++){
                                    bigIntegers[i] = BigInteger.valueOf(Long.parseLong(String.valueOf(objects[i])));
                                }
                                method.invoke(dx,(Object)bigIntegers);
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0]==BigDecimal[].class){
                            try {


                                Object[] objects = (Object[])value;
                                BigDecimal[] bigDecimals = new BigDecimal[objects.length];
                                for(int i = 0; i < bigDecimals.length; i++){
                                    bigDecimals[i] = new BigDecimal(String.valueOf(objects[i]));
                                }
                                method.invoke(dx,(Object)bigDecimals);


                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0]==Date[].class){
                            try {


                                Object[] objects = (Object[])value;
                                Date[] dates = new Date[objects.length];
                                for(int i = 0; i < dates.length; i++) {
                                    dates[i] = (Date) objects[i];
                                }
                                method.invoke(dx,(Object)dates);
                                break ws;
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }else if(c[0].isArray()){
                            Object[] objects = (Object[]) value;
                            for(int i = 0; i < objects.length; i++){
                                try {
                                    objects[i] = mapToObject(objects[i],Class.forName(c[0].toString().substring(8,c[0].toString().length()-1)));
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                            try {
                                method.invoke(dx,(Object)objects);
                            }catch (Exception e){
                                e.printStackTrace();
                            }


                        }else if(Collection.class.isAssignableFrom(c[0])){
                            String typeString = null;
                            try {
                                Field field = clazz.getDeclaredField(sKey);
                                if(field != null){
                                    ParameterizedType parameterizedType = (ParameterizedType)field.getGenericType();
                                    Type type = parameterizedType.getActualTypeArguments()[0];
                                    typeString = type.getTypeName();
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }


                            if(typeString==null){
                                if(value instanceof Collection){
                                    try {
                                        method.invoke(dx,value);
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }else {
                                    try {
                                        List list = new ArrayList();
                                        list.add(value);
                                        method.invoke(dx,list);
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                            }else {
                                try {
                                    Collection collection = (Collection)value;
                                    Collection collection1 = new ArrayList();
                                    Class clazz1 = Class.forName(typeString);
                                    Iterator iterator1 = collection.iterator();
                                    while (iterator1.hasNext()){
                                        Object o = iterator1.next();
                                        if(o instanceof Map&&!Map.class.isAssignableFrom(clazz1)){
                                            collection1.add(mapToObject(o,clazz1));
                                        }else {
                                            collection1.add(o);
                                        }
                                    }
                                    method.invoke(dx,collection1);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }


                            }
                            List list = (List) value;
                            for(int i = 0; i < list.size(); i++){
                                if(list.get(i) instanceof Map){
                                    try {
                                        Field field = clazz.getDeclaredField(sKey);
                                        if(field != null){
                                            ParameterizedType parameterizedType = (ParameterizedType)field.getGenericType();
                                            Type type = parameterizedType.getActualTypeArguments()[0];
                                            if(type != null){
                                                Object o = mapToObject(list.get(i),Class.forName(type.getTypeName()));


                                            }
                                        }
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }


                                    //c[0].getComponentType()
                                }
                            }
                        }
                        else {
                            if(value instanceof Map){
                                try {
                                    method.invoke(dx,mapToObject(value,c[0]));
                                    break ws;
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        }
                    }


                }
            }
        }
        return dx;
    }

猜你喜欢

转载自blog.csdn.net/qq_33770498/article/details/80524800