码迷,mamicode.com
首页 > 编程语言 > 详细

json转成java对象

时间:2015-08-18 13:45:54      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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     }  

 

json转成java对象

标签:

原文地址:http://www.cnblogs.com/xiaoqi/p/4738908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!