Android处理后台返回数据——Json转实体类
16lz
2021-01-23
本文出自:http://blog.csdn.net/dt235201314/article/details/69951885
代码下载:https://github.com/JinBoy23520/MPAndroidChartDemoByJin
(很好玩的Android图表Demo欢迎Star)
一丶慨述
通常开发场景:新的需求到来,产品经理整理好产品模型,召集后台,移动组(Android/iOS),UI设计师,测试开会,之后排开发时间。UI设计师先行(设计图),后台抓数据,
移动组记时间是在设计师出设计图之后正式开始,那么在设计师设计图的时候,移动组做什么呢,找控件,玩demo,而等设计师出设计图就开始实现效果,造数据。这里就说一下造数据。
二丶正文
后台是以json的形式将数据传给移动端。
先看一下关于json
Json由浅入深
上文介绍了json,同时也基本展示了后台可能给到移动组的数据类型。
在后台发布最新服务时,会先按照需求给移动组出一份需求文档,给出网络请求连接,方式,是否加密,请求参数,返回参数说明,还有请求报文(就是返回数据)。
这里主要说返回报文:
这个基本就是后代给出的返回参数,我们要根据这个写实体类,以及造数据
把返回报文复制到:http://www.json.cn/
这样就可以开始转了
/** * 下面json转实体类 */public void test(){ String data = "{\"realList\":[{\"amount\":\"3740\",\"month\":\"1\",\"year\":\"2017\"},{\"amount\":\"2382\",\"month\":\"2\",\"year\":\"2017\"},{\"amount\":\"3329\",\"month\":\"3\",\"year\":\"2017\"},{\"amount\":\"463\",\"month\":\"4\",\"year\":\"2017\"}],\n" + "\"yoyList\":[{\"amount\":\"4571\",\"month\":\"1\",\"year\":\"2016\"},{\"amount\":\"1630\",\"month\":\"2\",\"year\":\"2016\"},{\"amount\":\"2589\",\"month\":\"3\",\"year\":\"2016\"},{\"amount\":\"2180\",\"month\":\"4\",\"year\":\"2016\"},{\"amount\":\"3089\",\"month\":\"5\",\"year\":\"2016\"},{\"amount\":\"4906\",\"month\":\"6\",\"year\":\"2016\"},{\"amount\":\"5741\",\"month\":\"7\",\"year\":\"2016\"},{\"amount\":\"3611\",\"month\":\"8\",\"year\":\"2016\"},{\"amount\":\"2458\",\"month\":\"9\",\"year\":\"2016\"},{\"amount\":\"2608\",\"month\":\"10\",\"year\":\"2016\"},{\"amount\":\"5437\",\"month\":\"11\",\"year\":\"2016\"},{\"amount\":\"4219\",\"month\":\"12\",\"year\":\"2016\"}]}"; try { JSONObject object = new JSONObject(data); JSONArray jsonArray = object.getJSONArray("realList"); realList = new ArrayList<>(); for (int i = 0, count = jsonArray.length(); i < count; i++){ //改了这里 JSONObject jsonObject = jsonArray.optJSONObject(i);//{"amount":"3740","month":"1","year":"2017"} RealListEntity realListEntity = new RealListEntity(); String amount = jsonObject.optString("amount"); String month = jsonObject.optString("month"); String year = jsonObject.optString("year"); realListEntity.setAmount(amount); realListEntity.setMonth(month); realListEntity.setYear(year); realList.add(realListEntity); } JSONArray jsonArray1 = object.getJSONArray("yoyList"); yoyList = new ArrayList<>(); for (int i = 0, count = jsonArray1.length(); i < count; i++){ //改了这里 JSONObject jsonObject = jsonArray1.optJSONObject(i);//{"amount":"3740","month":"1","year":"2017"} YoyListEntity yoyListEntity = new YoyListEntity(); String amount = jsonObject.optString("amount"); String month = jsonObject.optString("month"); String year = jsonObject.optString("year"); yoyListEntity.setAmount(amount); yoyListEntity.setMonth(month); yoyListEntity.setYear(year); yoyList.add(yoyListEntity); } } catch (JSONException e) { e.printStackTrace(); }}
实体类例:
public class RealListEntity { private String year; private String month; private String amount; //销售数值 public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getAmount() { return amount; } public void setAmount(String amount) { this.amount = amount; }}
这样就可以直接运用报文的数据展示演示效果了
希望能给大家带来帮助!
更多相关文章
- 【Android】数据存储之Network
- day02 数据存储和界面展现(1)
- Android中的5种数据存储方式
- android 变长数据GSON解析
- android > SMS 短信数据库访问
- JNI基本数据类型转换
- Android摄像头视频帧数据测试代码
- android 如何在对话框中获取edittext中的数据
- android sqilte3数据库大小的测试