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

 static <T> List<T> parseArray(JSONArray arrary,Class<?> cls) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, ClassNotFoundException{
List<T> result = new ArrayList<T>();
String className = cls.getName();
for(int i=0;i<arrary.length();i++){
if(className.contains("java.lang")){
if(className.equals("java.lang.CharSequence") ||
className.equals("java.lang.String")) {
result.add((T) arrary.getString(i));
}else if(className.equals("java.lang.Double")) {
result.add((T) ((Double)arrary.getDouble(i)));
} else if(className.equals("java.lang.Integer")) {
result.add((T) ((Integer)arrary.getInt(i)));
} else if(className.equals("java.lang.Boolean")) {
result.add((T) ((Boolean)arrary.getBoolean(i)));
}
}else{
// 解析对象
result.add((T)json2Bean(arrary.getJSONObject(i),cls));
}
}
return result;
} public static <T> T json2Bean(JSONObject jsonObject, Class<?> cls) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException, InstantiationException, ClassNotFoundException {
// if (item == null) {
// return null;
// }
T item = (T) cls.newInstance();
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
String varName = field.getName();
if (jsonObject.has(varName)) {
Object value = jsonObject.get(varName); Class<?> currentClass = field.getType();
if(currentClass.equals(List.class)){
JSONArray array = (JSONArray)value;
String subClassName = field.getGenericType().toString().replace("java.util.List<", "");
subClassName = subClassName.substring(0,subClassName.length()-1);
// System.out.println(subClassName);
Class<?> clasz = Class.forName(subClassName);
// System.out.println(z.getClass());
BeanUtils.setProperty(item, varName, parseArray(array ,clasz)); }else{
if(value instanceof JSONObject){
BeanUtils.setProperty(item, varName, json2Bean((JSONObject)value,currentClass));
}else{
if(value instanceof JSONNull){
value = null;
}
BeanUtils.setProperty(item, varName, value);
}
}
}else{
// 设置默认值
//BeanUtils.setProperty(item, varName, null);
}
}
return item;
}

json转成java对象的更多相关文章

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

    net.sf.json.JSONObject为我们提供了toBean方法用来转换为JAVA对象, 功能更为强大,  这里借鉴采用JDK的反射机制, 作为简单的辅助工具使用,   有些数据类型需要进行转 ...

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

    @RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...

  3. json 串转成 java 对象再拼接成前台 html 元素

    获取商品参数 json 串,转成 java 对象,再拼接成前台 html 的Service方法 @Override public String getItemParam(Long itemId) { ...

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

    import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> ...

  5. 将String类型的json字符串转换成java对象

    1,import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); Mycl ...

  6. JSON字符串与java对象的转换

    所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...

  7. JSON 字符串 与 java 对象的转换

    jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //j ...

  8. Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...

  9. Java:Json与其他Java对象集合的转换

    一.引入的jar包 json-lib-2.4-jdk15.jar 二.Json字符串转换为其他对象 1.对象==>json字符串 2.list和Map集合==>json字符串 3.Map集 ...

随机推荐

  1. 转--python 面试题

    # 每一题都值得好好琢磨钻透 [原文地址](http://www.cnblogs.com/Allen-rg/p/7693394.html)1.Python是如何进行内存管理的? 答:从三个方面来说,一 ...

  2. 2.SpringBoot HelloWorld详解

    1.POM文件 父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. 20155332 2006-2007-2 《Java程序设计》第4周学习总结

    20155332 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 理解封装.继承.多态的关系 理解抽象类与接口的区别 掌握S.O.L.I.D原则 了解模式和设 ...

  4. asp.net mvc4 Json问题

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. Java 线性表、栈、队列和优先队列

    1.集合 2.迭代器 例子: 3.线性表 List接口继承自Collection接口,有两个具体的类ArrayList或者LinkedList来创建一个线性表 数组线性表ArrayList Linke ...

  6. Builder建造者模式

  7. python内置模块之unittest测试(五)

    系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python模块分析之typing(三) python模块分析之logging日志(四) pytho ...

  8. python函数——形参中的:*args和**kwargs

    python函数——形参中的:*args和**kwargs   多个实参,放到一个元组里面,以*开头,可以传多个参数:**是形参中按照关键字传值把多余的传值以字典的方式呈现 *args:(表示的就是将 ...

  9. springboot系列五、springboot常用注解使用说明

    一.controller相关注解 1.@Controller 控制器,处理http请求. 2.@RespController Spring4之后新加的注解,原来返回json需要@ResponseBod ...

  10. Html5 序列帧动画

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...