json数据转对象工具类
来源:互联网 发布:天气预报软件哪个最好? 编辑:程序博客网 时间:2024/06/16 22:42
一、定义系统常量
Constant.java
<span style="font-size:18px;">public final static String RETURN_CODE="rtnCode";//返回编码public final static String RETURN_MSG="rtnMsg";//返回信息public static final String DATA = "data";//返回数据public static final String PAGE_NO = "1";public static final String PAGE_SIZE = "10";</span>
二、两种格式的数据
1、data不是数组的情况
<span style="font-size:18px;">{"rtnMsg":"成功","rtnCode":"00","data":{"id":"12","name":"jack","age":67}}</span>
2、data是数组的情况
<span style="font-size:18px;">{"rtnMsg":"成功","rtnCode":"00","data":[{"id":"12","name":"jack","age":67}]}</span>
三、json解析工具类
<span style="font-size:18px;">/** * json转对象的实现 * @param jsonObj * @param clazz * @return */public static <T> T getObjectFromJsonObject(JSONObject jsonObj,Class<T> clazz){T t = null ;try {t = JSON.parseObject(JSONObject.toJSONString(jsonObj), clazz);} catch (Exception e) {e.printStackTrace();} return t; }/** * json转对象(data不是数组的情况) * @param jsonString * @param clazz * @return */public static <T> T getObjectFromJsonString(String jsonString,Class<T> clazz){ JSONObject jsonObject = null; JSONObject info = null; T t = null; if (StringUtils.isBlank(jsonString)) {return null;} try { jsonObject = JSONObject.parseObject(jsonString); if (!Constant.SUCCEED.equals(jsonObject.get(Constant.RETURN_CODE))) {return null;} info = jsonObject.getJSONObject(Constant.DATA); t = getObjectFromJsonObject(info,clazz); } catch (Exception e) { e.printStackTrace(); } return t; }/** * json转对象(data是数组的情况) * @param jsonString * @param clazz * @return */public static List<?> getObjectListFromJsonString(String jsonString,Class<?> clazz){ JSONObject jsonObject = null; JSONArray info = null; if (StringUtils.isBlank(jsonString)) {return null;} List list = new ArrayList(); try { jsonObject = JSONObject.parseObject(jsonString); if (!Constant.SUCCEED.equals(jsonObject.get(Constant.RETURN_CODE))) {return null;} info = jsonObject.getJSONArray(Constant.DATA); for (int i = 0; i < info.size(); i++) {list.add(getObjectFromJsonObject(info.getJSONObject(i),clazz));} } catch (Exception e) { e.printStackTrace(); } return list; }/** * json转对象(带分页的数据) * @param jsonString * @param clazz * @return */public static PageResult<?> getObjectListFromJsonStringWithPage(String jsonString,Class<?> clazz){ JSONObject jsonObject = null; JSONArray info = null; Integer pageNo = null; Integer pageSize = null; Integer totalCount = null; if (StringUtils.isBlank(jsonString)) {return null;} List list = new ArrayList(); try { jsonObject = JSONObject.parseObject(jsonString); if (!Constant.SUCCEED.equals(jsonObject.get(Constant.RETURN_CODE))) {return null;} pageNo = (Integer) jsonObject.get("pageNo"); pageSize = (Integer) jsonObject.get("pageSize"); if (pageSize==null) {pageSize = Integer.parseInt(Constant.PAGE_SIZE);} totalCount = (Integer) jsonObject.get("totalCount"); info = jsonObject.getJSONArray(Constant.DATA); for (int i = 0; i < info.size(); i++) {list.add(getObjectFromJsonObject(info.getJSONObject(i),clazz));} } catch (Exception e) { e.printStackTrace(); } PageResult pageResult = new PageResult(); pageResult.setPageSize(pageSize); pageResult.setPageOffSet(pageNo); pageResult.setTotalCount(totalCount); pageResult.setResultList(list); return pageResult; }</span>
PS:PageResult请参看此篇博客http://blog.csdn.net/lsq_401/article/details/50668890
0 0
- json数据转对象工具类
- Json对象工具类
- 对象转化json工具类
- iOS:数据转json对象
- java对象转json数据
- 一个关于Json数据List和对象嵌套反序列化的工具类,java(20151130)
- Objective - c JsonKit 进行json转对象 对象转json数据 & Jastor json数据转为对象
- 一个json对象相互转化工具类
- Json返回通用对象,工具类
- JOJSONBuilder工具类——一键把多个bean对象数据转换为JSON格式数据
- 转json工具类
- Android gson解析json数据工具类
- json数据转化工具类示例
- JSON转换工具类-JSON对象转换为字符串-json字符串转换为对象
- [转]js对象序列化成json数据
- JSON数据对象
- java对象转JSON JS取JSON数据
- handlebars获取json数据(集合对象)及各种自定义helper数据格式化工具
- HTTP协议
- android 获得当前的时间
- android进程间传递文件描述符原理
- 自己动手做推送
- python 汉诺塔
- json数据转对象工具类
- FlashBack总结之闪回数据库与闪回删除
- Linux中iostat命令参数详解
- HDU 4160 Dolls(DAG最小路径覆盖)
- leetcode(284) Peeking Iterator
- 【Android】安卓学习笔记之搭建安卓开发环境
- Linux查看CPU和内存使用情况
- OpenCV二次开发 编程规范
- nginx使用小结