查看json数据详情链接:/
相比json解析,Gson解析更高效简洁
先看看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);}
开始Gson解析
添加Gson依赖
implementation "com.google.code.gson:gson:2.8.2"
以一下json为解析对象
{"apkPath":"http://61.145.196.120/update/ywAPP/app-release.apk","version":26,"content":"1.修复自动更新bug"}
1、创建bean工具类
/*** 作者:created by meixi* 邮箱:13164716840@* 日期:/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、开始解析
String s = "{"apkPath":"http://61.145.196.120/update/ywAPP/app-release.apk","version":26,"content":"1.修复自动更新bug"}";
Gson gson = new Gson();TestGson userBean =gson.fromJson(s, new TypeToken<TestGson>() {}.getType());//TestGson userBean = gson.fromJson(s, TestGson.class);//第二种写法Log.i("lgq", "wwbb==" + userBean.getContent());
结果:11-02 09:43:53.055 18477-18477/com.tianxinyw.mapclient.liteapp I/lgq: wwbb==1.修复自动更新bug
3、Gson解析数组
{"result":1,"count":3,"page":1,"yeCount":1,"list":[{"readType":"未读","title":"大岭山光顺网吧计费即将到期,请提醒续费!","Startdate":"-11-16 23:59:59.0","placeCode":"1897"},{"readType":"未读","title":"大岭山玩不够网吧计费即将到期,请提醒续费!","Startdate":"-11-16 23:59:59.0","placeCode":"1887"},{"readType":"未读","title":"大朗盛佳网吧计费即将到期,请提醒续费!","Startdate":"-11-03 00:00:00.0","placeCode":"4701"}]}
3/1创建数组单元类
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;}}
内部类写法
public static class DataBean implements Serializable {private int id;private int device_id;
private List<DataBean> data;
3/2创建总数据元素类(可只取部分数据)
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开始解析json
Gson gson = new Gson();OutsideEntity userBean =gson.fromJson(s, new TypeToken<OutsideEntity>() {}.getType());List<list> userBeanList = userBean.getList();//单独获取数组集合Log.i("lgq", "re==logtest=====" +userBeanList.get(2).getTitle()+"...."+userBean.getYeCount());
结果是:11-02 13:41:47.907 14188-14188/com.tianxinyw.mapclient.liteapp I/lgq: re==logtest=====大朗盛佳网吧计费即将到期,请提醒续费!....1