Android 数据解析——Gson与json

查看json数据详情链接:JSON在线解析及格式化验证 - JSON.cn

相比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"
implementation 'com.google.code.gson:gson:2.9.1'

 

以一下json为解析对象

{"apkPath":"http://61.145.196.120/update/ywAPP/app-release.apk","version":26,"content":"1.修复自动更新bug"}

1、创建bean工具类

/*** 作者: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、开始解析

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":"2018-11-16 23:59:59.0","placeCode":"1897"},{"readType":"未读","title":"大岭山玩不够网吧计费即将到期,请提醒续费!","Startdate":"2018-11-16 23:59:59.0","placeCode":"1887"},{"readType":"未读","title":"大朗盛佳网吧计费即将到期,请提醒续费!","Startdate":"2018-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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415002.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

工作207:修改表头按钮样式

<el-card><div style"display: flex;justify-content: space-between"><h1 style"float: left;margin-top: 34px;margin-left: 32px;">我的任务</h1><el-button style"float: right;margin-top: 14px;margin-right: 10p…

TextView设置缩略显示

1、代码设置 textview.setSingleLine(); textview.setEllipsiz(TextUtils.TruncateAt.valueOf("END"));2、或在xml中设置&#xff1a;android:singleLine"true"android:ellipsize"end" 转载于:https://www.cnblogs.com/ouyangping/p/8158101.htm…

zsh: command not found: service

使用 services 指令&#xff0c;请先安装 brew tap gapple/services 安装完成后使用 brew services start mysql

Android 获取设备ID,手机厂商,运营商,联网方式,获取系统语言,获取时区

权限 <uses-permission android:name"android.permission.READ_PHONE_STATE" /> 安卓6.0需动态获取权限&#xff1a;Android 6.0及以上版本动态申请权限_meixi_android的博客-CSDN博客 获取设备ID 方法1 private String getAndroidId(){String m_szAndroidI…

str和unicode类

在py2中&#xff0c;分为两类&#xff0c;str和unicode 而在py3中&#xff0c;分为两类&#xff0c;byte和str py2中的str等同于py3中的byte 首先明确一点&#xff0c;我们编辑好一段文本&#xff0c;python并不知道我们的文本是以什么格式编码的。如果是纯英文字符还好说&…

Android 带阴影背景图片

1、添加依赖 compile com.dingmouren.paletteimageview:paletteimageview:1.0.7 2、引用 <com.dingmouren.paletteimageview.PaletteImageViewandroid:id"id/palette"android:layout_width"400dp"android:layout_height"400dp"android:lay…

Android画板控件,可以写字,签名,画画并生成图片

1效果图 实现步骤 1、添加画板控件module 画板控件module下载&#xff1a;https://download.csdn.net/download/meixi_android/10774781 2、xml文件 <?xml version"1.0" encoding"utf-8"?> <LinearLayoutandroid:id"id/content_main&q…

【网络流24题】餐巾计划问题(最小费用最大流)

【网络流24题】餐巾计划问题&#xff08;最小费用最大流&#xff09; 题面 COGS 洛谷上的数据范围更大&#xff0c;而且要开longlong 题解 餐巾的来源分为两种&#xff1a; ①新买的 ②旧的拿去洗 所以&#xff0c;两种情况分别建图 先考虑第一种 因为新买餐巾没有任何限制&…

js正则表达式匹配span标签

1、js正则表达式匹配span标签 const spans htmlStr.match(/<span (.*?)>(.*?)<\/span>/g)2、js正则表达式–获取标签内的文本 function fn(str) {return str.match(/<span[^>]*>([\s\S]*?)<\/span>/)[1] }

工作209:整理订单的重置逻辑

1点击新增 2找到子组件下面的混入 3找到混入 4这一块或许就是重置逻辑

phaser设置图片资源大小

核心代码如下&#xff1a; // 加载图片资源 this.load.image(pic6, /images/phaser/img/pic6.png) const pic this.add.image(1505, 630, pic6) pic.displayWidth 100 pic.displayHeight 100

位域操作

看runtime源码时&#xff0c;看到如下声明变量的&#xff0c;变量后分号前加冒号和数字": 数字"即为位域操作。 uintptr_t indexed : 1; 1个字节包含8位&#xff0c;有些变量保存的数据不需要占用这么长的空间&#xff08;比如bool类型&#xff0c;只有两个…

工作211:新的封装组件 秒呀

<!-- 可以动态新增的 tag 列表 --> <template><div><el-tagv-for"(tag, index) in dynamicTags":key"index":closable"true":disable-transitions"false"close"handleClose(tag)">{{ tag }}</el…

HTTP协议简介,数据安全 如何保证http传输安全性,http与https区别

目前大多数网站和app的接口都是采用http协议&#xff0c;但是http协议很容易就通过抓包工具监听到内容&#xff0c;甚至可以篡改内容&#xff0c;为了保证数据不被别人看到和修改&#xff0c;可以通过以下几个方面避免。 重要的数据&#xff0c;要加密&#xff0c;比如用户名密…

mongoose只更新数组中某一项的字段

只是需要一个特殊符号$代表匹配某一项 数据库设计如下&#xff1a; 核心代码如下&#xff1a; // 回答试题public async userPaperAnswer(request: IRequest, _h: IResponse) {const { user_paper_id, question_id, option_user } request.payload;const updOne await Use…

Koa2+Mysql搭建简易博客

http://blog.csdn.net/wclimb/article/details/77890793 转载于:https://www.cnblogs.com/SharkChilli/p/8177753.html

工作213:不能改变父组件值

父组件通过props传值给子组件&#xff0c;如何避免子组件改变props的属性值报错问题 报错Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop’s v…

uniapp移动H5在花生壳Invalid Host header

解决方案&#xff0c;在manifest.json中配置 "h5" : {"title" : "","domain" : "","devServer" : {"disableHostCheck" : true} }