API json多层数据实体类封装(利用泛型技术)
来源:互联网 发布:日本校园霸凌 知乎 编辑:程序博客网 时间:2024/05/22 12:21
完整源码查看点击进入:github地址
1: API json多层数据实体类封装(利用泛型技术)
RootEntity.java
public class RootEntity<T>{private int showapi_res_code;private String showapi_res_error;private PageBeanBody<T> showapi_res_body;//省略get set方法}
PageBeanBody.java
public class PageBeanBody<T> { private PageBean<T> pagebean; private int ret_code; //省略get set方法}
PageBean.java
public class PageBean<T> {private int allNum; private int allPages;private int currentPage;private int maxResult;private List<T> contentlist;//省略get set方法}
泛型方法
public static <T> RootEntity<T> parseNewsList(Response response,Class<T> mClazz) { JSONObject root=JSON.parseObject(response.getResponseText()); JSONObject body=JSON.parseObject(response.getResponseText()).getJSONObject("showapi_res_body"); JSONObject page=JSON.parseObject(response.getResponseText()).getJSONObject("showapi_res_body").getJSONObject("pagebean"); String contentList=JSON.parseObject(response.getResponseText()).getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist").toJSONString(); //实例化分页类 PageBean<T> pageBean=new PageBean<T>(); pageBean.setContentlist(JSON.parseArray(contentList, mClazz));//可能报错 pageBean.setAllNum(page.getIntValue("allNum")); pageBean.setAllPages(page.getIntValue("allPages")); pageBean.setCurrentPage(page.getIntValue("currentPage")); pageBean.setMaxResult(page.getIntValue("maxResult")); //实例化Body类 PageBeanBody<T> pageBeanBody=new PageBeanBody<T>(); pageBeanBody.setPagebean(pageBean); pageBeanBody.setRet_code(body.getIntValue("ret_code")); //实例化Root类 RootEntity<T> rootEntity=new RootEntity<T>(); rootEntity.setShowapi_res_body(pageBeanBody); rootEntity.setShowapi_res_code(root.getIntValue("showapi_res_code")); rootEntity.setShowapi_res_error(root.getString("showapi_res_error")); return rootEntity; }
调用泛型方法
public static void loadNewsList(String url, Map<String, Object> param, LinkedHashMap<String, Object> headers, String bodyJson, String post) { Response response = StaticUtil.commomHttpMethod(url, param, headers, bodyJson, post); //调用泛型方法 RootEntity<NewEntity> data= parseNewsList(response, NewEntity.class); System.out.println("desc:"+data.getShowapi_res_body().getPagebean().getContentlist().get(0).getImageurls().size()); System.out.println("code:"+data.getShowapi_res_code()); System.out.println("error:"+data.getShowapi_res_error()); System.out.println("allnum:"+data.getShowapi_res_body().getPagebean().getAllNum()); System.out.println("allpages:"+data.getShowapi_res_body().getPagebean().getAllPages()); System.out.println("currentpages:"+data.getShowapi_res_body().getPagebean().getCurrentPage()); System.out.println("maxresult:"+data.getShowapi_res_body().getPagebean().getMaxResult()); }
0 0
- API json多层数据实体类封装(利用泛型技术)
- 利用Handler实现网络数据下载Json并转换成实体类的封装
- java封装多层json
- 利用泛型实现通用的数据实体类填充
- 利用Gson解析多层嵌套的JSON数据
- Json数据封装到相应的实体类中(工具类)
- GSON解析json含有不确定的泛型实体类,根据键值获取json数据
- 泛型解析JSON数据封装
- C# 请求Web Api 接口,返回的json数据直接反序列化为实体类
- 所有实体Bean中用泛型和反射技术对分页技术的封装
- 提取多层嵌套Json数据
- 多层json数据编码转换
- jQuery遍历多层json数据
- 提取多层嵌套Json数据
- 提取多层嵌套Json数据
- 封装数据库操作---数据实体操作封装(一)
- Json数据转换成实体类
- 利用反射将数据读入实体类
- 工信部《VR产业白皮书》全文 官方解读虚拟现实
- hrbust 2104 Encryption 【模拟+STL】
- javascript Date format(js日期格式化)
- mac android studio 离线文档
- ASP.NET MVC Controller的激活
- API json多层数据实体类封装(利用泛型技术)
- [bzoj4522][CQOI2016]密钥破解
- ISE数字设计入门体验
- 【NOIP2015模拟11.3】备用钥匙
- Android架构合集(文章整理,后续不断更新中)
- Eclipse 下找不到或无法加载主类的解决办法
- Agileone安装
- Android屏幕适配全攻略
- 36. Valid Sudoku