JSON 、复杂对象转换


 public static Object copyProperties(Object jsonObject, Object desObj) {
        //1. 首先要拿到oriObj对象的所有存在值不为空的键-值,放入到集合当中
        if (jsonObject instanceof JSONObject) {
            JSONObject oriObj = (JSONObject) jsonObject;
            Class desC = desObj.getClass();
            Field[] oField = desC.getDeclaredFields();
            Class pclazz = desC.getSuperclass();
            Field[] pFields = pclazz.getDeclaredFields();
            int  osize  = oField.length;
            int  psize  = pFields.length;

            Field[] desFields  = new  Field[osize+ psize];
            for(int  i=  0; i <osize+ psize ; i ++ ){
                if(i < osize ){
                    desFields[i] = oField[i];
                }else {
                    desFields[i] = pFields[i -osize ];
                }
            }
            for (Field desField : desFields) {
                String fieldName = desField.getName();
                String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
                String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
                if (oriObj.get(fieldName) != null) {
                    Object or = oriObj.get(fieldName);
                    if (or instanceof JSONObject) {
                        try {
                            Class type = desField.getType();
                            if (type.getName().indexOf("[") == 0) {
                                continue;
                            }
                            Method getMethod = desC.getMethod(getMethodName, null);


                            Object ch =  getMethod.invoke(desObj);
                            if (or instanceof JSONObject || or instanceof JSONArray) {
                                copyProperties(or, ch);
                            }
                            Method setMethod = desObj.getClass().getMethod(setMethodName, type);
                            setMethod.invoke(desObj, ch);
                        }  catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if (or instanceof JSONArray) {
                        try {
                            Class type = desField.getType();
                            if (type.getName().indexOf("[") != 0) {
                                continue;
                            }
                            Method getMethod = desC.getMethod(getMethodName, null);
                            JSONArray jsonArray = (JSONArray) or;
                            Object[] o = (Object[]) getMethod.invoke(desObj);
                            if(o ==null){
                                Class ctype = Class.forName(type.getName().replace("[L","").replace(";",""));
                                  o = (Object[]) Array.newInstance(ctype,jsonArray.size());
                                    for(int i =  0 ; i < o.length; i ++){
                                      o[i] =  ctype.newInstance();

                                  }
                            }
                            Object   o1 =   copyProperties(jsonArray, o);
                            Method setMethod = desObj.getClass().getMethod(setMethodName, type);
                            setMethod.invoke(desObj, o1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            Class type = desField.getType();
                            Object pa = oriObj.getObject(fieldName,type);
                            Method setMethod = desObj.getClass().getMethod(setMethodName, type);
                            setMethod.invoke(desObj,  pa );
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }else if  (jsonObject instanceof JSONArray){
            JSONArray jsonArray = (JSONArray) jsonObject;
            try {
                Class type = desObj.getClass();
                if (type.getName().indexOf("[") != 0) {
                    return  null ;
                }

                Object[] ob = (Object[]) desObj;

                Integer size = jsonArray.size();
                if(ob.length != size){
                    ob = (Object[]) Array.newInstance(type,size);
                }
                for(int i = 0 ; i< size ; i ++ ){
                    Object o = jsonArray.get(i);
                    if(o instanceof JSONObject) {
//                        Object co =  Class.forName(type.getCanonicalName().replace("[","").replace("]","")).newInstance();
                        ob[i] =  copyProperties(o, ob[i] );;
                    }
                }

                return  ob;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return desObj;
    }

        public static void wirteJson (Object desObj, JSONObject retJson) throws
        NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            //1. 首先要拿到oriObj对象的所有存在值不为空的键-值,放入到集合当中

            String a = "";
            Class desC = desObj.getClass();
            Field[] desFields = desC.getDeclaredFields();
            Class pclazz = desC.getSuperclass();
            Field[] pFields = pclazz.getDeclaredFields();
            for (Field desField : desFields) {
                Class c = desField.getType();

                String fieldName = desField.getName();
                String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
                try {
                    Method getMethod = desC.getMethod(getMethodName, null);

                    if (c.getName().indexOf("[") == 0) {
                        /**
                         * t
                         */
                        Object[] obs = (Object[]) getMethod.invoke(desObj);//获取得到该属性的取值
                        if (obs == null) {
                            continue;
                        }
                        JSONArray cjson = new JSONArray();
                        for (Object ob : obs) {
                            JSONObject cn = new JSONObject();
                            wirteJson(ob, cn);
                            cjson.add(cn);
                        }
                        retJson.put(fieldName, cjson);


                    } else {
                        Object ob = getMethod.invoke(desObj);//获取得到该属性的取值
                        if (ob == null) {
                            continue;
                        }
                        JSONObject cjson = new JSONObject();
                        Field[] cfs = ob.getClass().getDeclaredFields();
                        boolean b = false;
                        for (Field cf : cfs) {
                            String cfieldName = cf.getName();
                            String getCMethodName = "get" + cfieldName.substring(0, 1).toUpperCase() + cfieldName.substring(1, cfieldName.length());
                            try {
                                Method getCMethod = ob.getClass().getMethod(getCMethodName);
                                b = true;
                                break;
                            } catch (Exception e) {

                            }
                        }
                        if (b) {
                            wirteJson(ob, cjson);
                            retJson.put(fieldName, cjson);
                        }
                        retJson.put(fieldName, ob);

                    }
                } catch (Exception e) {
                    logger.error(fieldName + "get方法获取失败");
                }


            }
        }

  

posted @ 2022-09-28 16:54  higsan  阅读(234)  评论(0编辑  收藏  举报