json转换成list
来源:互联网 发布:tomcat连接不上数据库 编辑:程序博客网 时间:2024/06/10 01:31
/** * json转成list列表. * * @param data * json字符串 * @param clazz * 类型 * @param <T> * 泛型 * @return 列表 * @throws Exception * 可能抛出任何异常 */ public static <T extends Object> List<T> json2List(String data, Class<T> clazz) throws Exception { return json2List(data, clazz, null, null); }/** * data=[{"id":"1"},{"id":2}]用json里的数据,创建pojo队列. * * @param <T> * Object * @param data * json字符串 * @param clazz * 需要转换成node的具体类型 * @param excludes * 不需要转换的属性数组 * @param datePattern * 日期转换模式 * @return List * @throws Exception * java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException */ public static <T extends Object> List<T> json2List(String data, Class<T> clazz, String[] excludes, String datePattern) throws Exception { JSONArray jsonArray = JSONArray.fromObject(data); return json2List(jsonArray, clazz, excludes, datePattern); } /** * data=[{"id":"1"},{"id":2}]用json里的数据,创建pojo队列. * * @param <T> * Object * @param jsonArray * JSONArray * @param clazz * 需要转换成node的具体类型 * @param excludes * 不需要转换的属性数组 * @param datePattern * 日期转换模式 * @return List * @throws Exception * java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException */ public static <T extends Object> List<T> json2List(JSONArray jsonArray, Class<T> clazz, String[] excludes, String datePattern) throws Exception { List<T> list = new ArrayList<T>(); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); T node = json2Bean(jsonObject, clazz, excludes, datePattern); list.add(node); } return list; }/** * 根据Class生成entity,再把JSONObject中的数据填充进去. * * @param <T> * Object * @param jsonObject * json对象 * @param clazz * 需要转换成bean的具体类型 * @param excludes * 不需要转换的属性数组 * @param datePattern * 日期转换模式 * @return T * @throws Exception * java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException */ public static <T extends Object> T json2Bean(JSONObject jsonObject, Class<T> clazz, String[] excludes, String datePattern) throws Exception { // JsonUtils.configJson(excludes, datePattern); T entity = clazz.newInstance(); return json2Bean(jsonObject, entity, excludes, datePattern); } /** * 把JSONObject中的数据填充到entity中. * * @param <T> * Object * @param jsonObject * json对象 * @param entity * 需要填充数据的node * @param excludes * 不需要转换的属性数组 * @param datePattern * 日期转换模式 * @return T * @throws Exception * java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException */ @SuppressWarnings("rawtypes") public static <T extends Object> T json2Bean(JSONObject jsonObject, T entity, String[] excludes, String datePattern) throws Exception { // JsonUtils.configJson(excludes, datePattern); Set<String> excludeSet = createExcludeSet(excludes); for (Object object : jsonObject.entrySet()) { Map.Entry entry = (Map.Entry) object; String propertyName = entry.getKey().toString(); if (excludeSet.contains(propertyName)) { continue; } String propertyValue = entry.getValue().toString(); try { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, entity.getClass()); Class propertyType = propertyDescriptor.getPropertyType(); Method writeMethod = propertyDescriptor.getWriteMethod(); invokeWriteMethod(entity, writeMethod, propertyType, propertyValue, datePattern); } catch (IntrospectionException ex) { logger.info(entity.getClass() + ":" + ex.getMessage()); continue; } } return entity; } /** * 配置排除列表. * * @param excludes * String[] * @return exclude set */ public static Set<String> createExcludeSet(String[] excludes) { Set<String> excludeSet = new HashSet<String>(); if (excludes != null) { for (String exclude : excludes) { excludeSet.add(exclude); } } else { excludeSet.add("hibernateLazyInitializer"); } return excludeSet; } /** * 根据类型,反射调用setter方法. * * @param entity * 实例 * @param writeMethod * setter方法 * @param propertyType * 数据类型 * @param propertyValue * 数据值 * @param datePattern * 日期格式 * @throws IntrospectionException * methed * @throws Exception * e */ @SuppressWarnings("rawtypes") public static void invokeWriteMethod(Object entity, Method writeMethod, Class propertyType, String propertyValue, String datePattern) throws IntrospectionException, Exception { try { /** 如果参数为空 则 不再做处理 **/ if (!StringUtil.notNullorEmpty(propertyValue)) { return; } /** 验证八个基本类型 **/ if (isPrimivite(propertyType)) { invokePrimivite(entity, writeMethod, propertyType, propertyValue); /** 验证String类型 **/ } else if (propertyType == String.class) { writeMethod.invoke(entity, propertyValue); /** 验证date类型 **/ } else if (propertyType == Date.class && StringUtil.notNullorEmpty(propertyValue) && !"null".equals(propertyValue)) { SimpleDateFormat dateFormat = getDateFormat(datePattern); /** 如果datePattern为空,SimpleDateFormat 的默认格式为 yyyy-MM-dd T HH:mm:ss **/ try { /** 如果 采用默认格式 则 容易转换失败 如:(2011-11-11) **/ writeMethod.invoke(entity, dateFormat.parse(propertyValue)); } catch (ParseException e) { /** 如果 转换格式失败 则 采用年月日格式 **/ writeMethod.invoke(entity, getDateFormat("yyyy-MM-dd").parse(propertyValue)); } } } catch (IntrospectionException e) { /** 转为bean的json信息 里包含了大量其他参数信息 没有找到weiterMethod将很常见 此处起提示作用 **/ throw new IntrospectionException("没有找到" + writeMethod + "方法!"); } catch (Exception exception) { /** 如果包含其他异常将记录并向上抛出 **/ logger.error(exception); throw new Exception(exception); } }
0 0
- list 转换成json
- JSON转换成LIST
- json转换成list
- List类型转换成JSON
- RestultSet 转换成List、Json
- 将list转换成Json
- List转换成JSON对象
- 将list转换成json
- 控制器json转换成list
- JSON--List集合转换成JSON对象
- JSON--List集合转换成JSON对象
- JSON--List集合转换成JSON对象
- JSON--List集合转换成JSON对象
- JSON--List集合转换成JSON对象
- android中json转换成List<Map>
- 把json字符串转换成List
- c# list集合转换成json数据
- 将list对象转换成json格式
- [DB2]DB2备份和恢复
- 用spring-session来管理http session
- Linux每日学习(一)
- 页面响应时间,请求响应时间
- ant实现java项目动态构建与部署
- json转换成list
- 基于Source和正则表达式的Scala网页内容抓取
- Ubuntu 下用NDK编译移植 FFmpeg 2.0 (配置最新版x264) 到Android平台
- Mybatis 动态sql
- iOS开发 打印CGRect时用NSStringFromCGRect
- 【Codeforces Round 273 (Div 2)C】【贪心 脑洞】 Table Decorations 三种气球装饰桌子 同桌三个气球不完全同色的最多桌子装饰数
- 【LEETCODE】241-Different Ways to Add Parentheses
- 转载一篇android裁剪图片的解决方案,个人呢觉得很好的
- 基于暗通道去雾算法的实现与优化(二)opencv在pc上的实现