in android json serialization and deserialization

After recent projects will need to turn json string objects in android and deserialize objects, access to the android sdk documentation and found no similar android DataContractJsonSerializer classes in C #, think demand is not too complicated, you write your own the two solutions, as follows:

public  static String getJson(Object objSource){
         try{
            Object object = getJsonObject(objSource);
             if(object ==  null)
                 return "";
             return ((JSONObject)object).toString();
        } catch(Exception err){
            Log.e(TAG, "getJson exception:"+err.getMessage());
             return "";
        }
    }
    
     private  static Object getJsonObject(Object objSource)  throws Exception{
         if(objSource !=  null){            
            Class<?> objClass = objSource.getClass();
             if(objClass.getPackage().getName().startsWith(PackageString)){
                JSONObject jsonObject =  new JSONObject();
                Field[] publicFields = objClass.getFields();
                Object val;            
                 for(Field field : publicFields){
                    val = field.get(objSource);
                     if(val !=  null){    
                        Class<?> valClass = val.getClass();
                         if(valClass.isArray()){
                            JSONArray array =  new JSONArray();
                             int length = Array.getLength(val);
                             for( int i = 0; i<length; i++){
                                array.put(getJsonObject(Array.get(val, i)));
                            }
                            jsonObject.put(field.getName(), array);
                        } else  if(valClass.equals(ArrayList. class)){
                            ArrayList<?> aList = ArrayList. class.cast(val);
                            Iterator<?> iterator = aList.iterator();
                            JSONArray array =  new JSONArray();
                             while (iterator.hasNext()) {
                                array.put(getJsonObject(iterator.next()));                                
                            }
                            jsonObject.put(field.getName(), array);
                        } else{
                            jsonObject.put(field.getName(),getJsonObject(val));
                        }
                    } else{
                        jsonObject.put(field.getName(),  null);
                    }                
                }
                 return jsonObject;
            } else{
                 return objSource;
            }
        }
         return  null;
    }
    
    
     public  static Object parseJson(Class<?> targetClass,String jsonString){
         try{
            Object object = targetClass.newInstance();
            JSONObject jsonObject =  new JSONObject(jsonString);
            String key;
            Class<?> fieldClass;
            Object element;
            Class<?> elementType;
            Field[] fields = targetClass.getFields();
            
             for(Field field : fields){
                
                key = field.getName();
                fieldClass = field.getType();
                 if(fieldClass.equals(ArrayList. class)){
                    String genericType = field.getGenericType().toString();
                     if(genericType.indexOf("<") > 0){
                        genericType = genericType.substring(genericType.indexOf("<")+1,genericType.lastIndexOf(">"));    
                    }
                    ArrayList<Object> aList =  new ArrayList<Object>();
                    JSONArray array = jsonObject.getJSONArray(key);
                     int length = array.length();
                     if(genericType.startsWith(PackageString)){
                        elementType = Class.forName(genericType);
                        Field[] elementFields = elementType.getFields();
                        JSONObject subJsonObject;
                         for( int i=0;i<length;i++){
                            element = elementType.newInstance();
                            subJsonObject = array.getJSONObject(i);
                             for(Field subField : elementFields){
                                subField.set(element, subJsonObject.get(subField.getName()));
                            }
                            aList.add(element);                        
                        }
                    } else{
                         for( int i=0;i<length;i++){
                            aList.add(array.get(i));
                        }
                    }
                    field.set(object, aList);
                    
                } else  if(fieldClass.isArray()){
                    JSONArray array = jsonObject.getJSONArray(key);
                     int length = array.length();
                    String arrayTypeName = fieldClass.getComponentType().getName();
                    Object[] objArray =  new Object[length];
                     
                     if(arrayTypeName.startsWith(PackageString)){
                        elementType = Class.forName(arrayTypeName);
                        Field[] elementFields = elementType.getFields();
                        JSONObject subJsonObject;
                         for( int i=0;i<length;i++){
                            element = elementType.newInstance();
                            subJsonObject = array.getJSONObject(i);
                             for(Field subField : elementFields){
                                subField.set(element, subJsonObject.get(subField.getName()));
                            }
                            objArray[i] = element;                        
                        }
                    } else{
                         for( int i=0;i<length;i++){
                            objArray[i] = array.get(i);
                        }
                    }
                    field.set(object, objArray);
                    
                } else{
                    field.set(object, jsonObject.get(key));
                }
            }
             return object;
        } catch(Exception err){
            Log.e(TAG, "parseJson exception:"+err.getMessage());
             return  null;
        }
    }

 Supports basic types, arrays, ArrayList members of the class

Reproduced in: https: //www.cnblogs.com/hdjjun/archive/2012/08/30/2663981.html

Guess you like

Origin blog.csdn.net/weixin_33895604/article/details/94497360