• json转成java对象


    avro生成的代码里,String是CharSequence,不能通过Gson反序列化,于是有了下面的代码,ParseArray里还不完善:

     1 static <T> List<T> parseArray(JSONArray arrary,Class<?> cls) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, ClassNotFoundException{
     2       List<T> result = new ArrayList<T>();
     3       String className = cls.getName();
     4       for(int i=0;i<arrary.length();i++){ 
     5           if(className.contains("java.lang")){          
     6               if(className.equals("java.lang.CharSequence") ||
     7                       className.equals("java.lang.String")) {
     8                   result.add((T) arrary.getString(i));
     9               }else  if(className.equals("java.lang.Double")) {
    10                   result.add((T) ((Double)arrary.getDouble(i)));
    11               }  else  if(className.equals("java.lang.Integer")) {
    12                   result.add((T) ((Integer)arrary.getInt(i)));
    13               }  else  if(className.equals("java.lang.Boolean")) {
    14                   result.add((T) ((Boolean)arrary.getBoolean(i)));
    15               }  
    16           }else{
    17               // 解析对象
    18               result.add((T)json2Bean(arrary.getJSONObject(i),cls));
    19           }     
    20       }  
    21       return result;
    22   }
    23 
    24   public static <T> T json2Bean(JSONObject jsonObject, Class<?> cls) throws IllegalAccessException,  
    25   InvocationTargetException, NoSuchMethodException, InstantiationException, ClassNotFoundException {  
    26 //        if (item == null) {  
    27 //          return null;  
    28 //        }  
    29         T item = (T) cls.newInstance();
    30         Field[] fields = cls.getDeclaredFields();  
    31         for (Field field : fields) {  
    32           String varName = field.getName();  
    33           if (jsonObject.has(varName)) {
    34               Object value = jsonObject.get(varName); 
    35 
    36              Class<?> currentClass = field.getType();
    37              if(currentClass.equals(List.class)){
    38                  JSONArray array = (JSONArray)value;
    39                 String subClassName = field.getGenericType().toString().replace("java.util.List<", "");
    40                 subClassName = subClassName.substring(0,subClassName.length()-1);
    41 //                System.out.println(subClassName);                                    
    42                 Class<?> clasz =    Class.forName(subClassName);
    43 //                System.out.println(z.getClass());
    44                 BeanUtils.setProperty(item, varName, parseArray(array ,clasz));
    45             
    46              }else{
    47                  if(value instanceof JSONObject){
    48                      BeanUtils.setProperty(item, varName, json2Bean((JSONObject)value,currentClass));  
    49                  }else{
    50                     if(value instanceof JSONNull){
    51                         value = null;
    52                     }
    53                    BeanUtils.setProperty(item, varName, value);  
    54                  }
    55              }
    56           }else{
    57               // 设置默认值
    58               //BeanUtils.setProperty(item, varName, null);    
    59           }
    60         }  
    61          return item;  
    62     }  
  • 相关阅读:
    【leetcode】21-MergeTwoSortedLists
    【leetcode】20-ValidParentheses
    【c++基础】遍历目录并写入txt文件-linux
    【c++基础】字符数组和string相互转换
    【linux基础】使用命令行编译运行c++程序
    第2章 重新组织函数(4):函数对象、替换算法
    第2章 重新组织函数(3):引入解释性变量、分解临时变量和移除对参数的赋值
    第2章 重新组织函数(2):内联函数、内联临时变量和查询函数
    第2章 重新组织函数(1):提炼函数
    第1章 重构,第一个案例(3):运用多态取代switch
  • 原文地址:https://www.cnblogs.com/xiaoqi/p/4738908.html
Copyright © 2020-2023  润新知