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     }  
时间: 2024-12-24 19:03:12

json转成java对象的相关文章

【代码分享——Java&amp;Json】Json转成java对象,已经java对象转成Json对象

做记录用,肯定有地方不完整,先放着吧 [代码分享--Java&Json]Json转成java对象,已经java对象转成Json对象,布布扣,bubuko.com

利用JAVA反射机制将JSON数据转换成JAVA对象

net.sf.json.JSONObject为我们提供了toBean方法用来转换为JAVA对象, 功能更为强大,  这里借鉴采用JDK的反射机制, 作为简单的辅助工具使用,   有些数据类型需要进行转换, 根据需要进行扩展,  这里可以处理Long和Date类型. 只支持单个JSONObject对象的处理,   对于复杂的JSON对象, 如JSONArray数组, 可考虑先遍历, 获取JSONObject后再进行处理. package common; import java.lang.refle

json字符串转json对象,json对象转换成java对象

@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @ResponseBody public void updateInvestorApplyAccountNo(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) { int num = 0;

json字符串转成 json对象 json对象转换成java对象

import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version></dependency> String r

JSON 字符串 与 java 对象的转换

jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //json-lib-2.2-jdk15.jar import net.sf.json.JSONArray;import net.sf.json.JSONObject; 1. //把java 对象列表转换为json对象数组,并转为字符串 JSONArray array = JSONArray.fromOb

Json对象与Json字符串的转化、JSON字符串与Java对象的转换

Json对象与Json字符串的转化.JSON字符串与Java对象的转换 一.Json对象与Json字符串的转化 1.jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 2.浏览器支持的转换方式(Firefox,chrome,opera,safari,ie9,ie8)等浏览器: JSON.stringify(obj)将JSON转为字符串.JSON.parse(string)

JSON字符串与java对象的转换

所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar 1. 把java 对象列表转换为json对象数组,并转为字符串 JSONArray array = JSONArray.fromObject(userlist); String jsonstr = array.toSt

JSON 字符串 与 java 对象之间互相转换

首先引入maven需要依赖的jar: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> 需要导入的类: import net.sf.json

JSON序列化为java对象

一. 前台(JS  面向对象) 1. 定义SearchView对象 function SearchView() { } SearchView.prototype.setViewName = function (viewName) {     this.viewName = viewName; } SearchView.prototype.setViewType = function(viewType) {     this.viewType = viewType; } SearchView.pr