Android 数据解析——Gson与json
查看json數(shù)據(jù)詳情鏈接:https://www.json.cn/
?
相比json解析,Gson解析更高效簡(jiǎn)潔
先看看json解析
JSONObject object = new JSONObject(s);String succ= object.getString("success"); JSONArray barray = object.getJSONArray("list"); for (int b = 0;b<barray.length();b++){JSONObject jsonObject = barray.getJSONObject(b);String title = jsonObject.getString("title");String Startdate = jsonObject.getString("Startdate");String placeCode = jsonObject.getString("placeCode");String readType = jsonObject.getString("readType");if (jsonObject.getString("readType").equals("未讀")){}MiniNewsEntity entity1 = new MiniNewsEntity();entity1.setStartdate(Startdate);entity1.setTitle(title);entity1.setPlaceCode(placeCode);entity1.setStatus(readType);barEntityList.add(entity1); }?
開(kāi)始Gson解析
添加Gson依賴(lài)
implementation "com.google.code.gson:gson:2.8.2"以一下json為解析對(duì)象
{"apkPath":"http://61.145.196.120/update/ywAPP/app-release.apk","version":26,"content":"1.修復(fù)自動(dòng)更新bug"}
?
1、創(chuàng)建bean工具類(lèi)
/*** 作者:created by meixi* 郵箱:13164716840@163.com* 日期:2018/11/2 09*/ public class TestGson {private String apkPath;private String version;private String content;public String getApkPath() {return apkPath;}public void setApkPath(String apkPath) {this.apkPath = apkPath;}public String getVersion() {return version;}public void setVersion(String version) {this.version = version;}public String getContent() {return content;}public void setContent(String content) {this.content = content;} }?
2、開(kāi)始解析
String s = "{"apkPath":"http://61.145.196.120/update/ywAPP/app-release.apk","version":26,"content":"1.修復(fù)自動(dòng)更新bug"}";
?
Gson gson = new Gson();TestGson userBean =gson.fromJson(s, new TypeToken<TestGson>() {}.getType());// TestGson userBean = gson.fromJson(s, TestGson.class);//第二種寫(xiě)法Log.i("lgq", "wwbb==" + userBean.getContent());?
結(jié)果:11-02 09:43:53.055 18477-18477/com.tianxinyw.mapclient.liteapp I/lgq: wwbb==1.修復(fù)自動(dòng)更新bug
?
3、Gson解析數(shù)組
?
{"result":1,"count":3,"page":1,"yeCount":1,"list":[{"readType":"未讀","title":"大嶺山光順網(wǎng)吧計(jì)費(fèi)即將到期,請(qǐng)?zhí)嵝牙m(xù)費(fèi)!","Startdate":"2018-11-16 23:59:59.0","placeCode":"1897"},{"readType":"未讀","title":"大嶺山玩不夠網(wǎng)吧計(jì)費(fèi)即將到期,請(qǐng)?zhí)嵝牙m(xù)費(fèi)!","Startdate":"2018-11-16 23:59:59.0","placeCode":"1887"},{"readType":"未讀","title":"大朗盛佳網(wǎng)吧計(jì)費(fèi)即將到期,請(qǐng)?zhí)嵝牙m(xù)費(fèi)!","Startdate":"2018-11-03 00:00:00.0","placeCode":"4701"}]}
?
3/1創(chuàng)建數(shù)組單元類(lèi)
public class list {private String readType;private String title;private String Startdate;public String getReadType() {return readType;}public void setReadType(String readType) {this.readType = readType;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getStartdate() {return Startdate;}public void setStartdate(String startdate) {Startdate = startdate;} }內(nèi)部類(lèi)寫(xiě)法
public static class DataBean implements Serializable {private int id;private int device_id;?
private List<DataBean> data;?
3/2創(chuàng)建總數(shù)據(jù)元素類(lèi)(可只取部分?jǐn)?shù)據(jù))
?
public class OutsideEntity {private String yeCount;private List<com.tianxinyw.mapclient.entitys.list> list = new ArrayList<>();public String getYeCount() {return yeCount;}public void setYeCount(String yeCount) {this.yeCount = yeCount;}public List<com.tianxinyw.mapclient.entitys.list> getList() {return list;}public void setList(List<com.tianxinyw.mapclient.entitys.list> list) {this.list = list;} }?
3/3開(kāi)始解析json
Gson gson = new Gson();OutsideEntity userBean =gson.fromJson(s, new TypeToken<OutsideEntity>() {}.getType());List<list> userBeanList = userBean.getList();//單獨(dú)獲取數(shù)組集合Log.i("lgq", "re==logtest=====" +userBeanList.get(2).getTitle()+"...."+userBean.getYeCount());結(jié)果是:11-02 13:41:47.907 14188-14188/com.tianxinyw.mapclient.liteapp I/lgq: re==logtest=====大朗盛佳網(wǎng)吧計(jì)費(fèi)即將到期,請(qǐng)?zhí)嵝牙m(xù)費(fèi)!....1
總結(jié)
以上是生活随笔為你收集整理的Android 数据解析——Gson与json的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何制作pem公钥证书和私钥证书
- 下一篇: HenCoder 3-1 触摸反馈,以及