Android用Gson解析JSON字符串
在volley框架中有一个 protected Response<Result<T>> parseNetworkResponse(NetworkResponse response){}函数。从服务器上或者在缓存中获取的JSON字符串在这个函数进行解析。
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, HTTP.UTF_8))
Result<T> result = GsonUtil.getResult(jsonString, mTypeToken);
我们可以定义一个GsonUtil类。里面写一些Gson和JSON相互转化的功能函数。例如上用到的getResult函数。利用该函数获取的Result<T>这个JavaBean。也就是返回的规范,该规范由前端开发人员和后台开发人员商量得出。既然得到的JavaBean,那么我再要获取JSON里面的数据就像从一个类中获取一个数据一样简单了。
public static <T> T getResult(String jsonData, TypeToken<T> type) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
return (T) gson.fromJson(jsonData, type.getType());
}
Result<T>类:例如:(后台返回的JSON字符串形如:{"err":"success","errno":1,"rsm{"userId":"1","email":"?","password":"?","firstName":"胡","lastName":"安","gender":"1","photo":"/picture/userPhoto/1422624411423.PNG","autograph":"","introduce":"","createTime":"4天前"}}失败返回{"err":"用户不存在!",errno":0} )
package whu.cn.whushare.bean; import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName; public class Result<T> {
@Expose
@SerializedName("currentPage")
private long currentPage;
@Expose
@SerializedName("pageSize")
private long pageSize;
@Expose
@SerializedName("allDataCount")
private long allDataCount;
@Expose
@SerializedName("nextPage")
private long nextPage;
@Expose
@SerializedName("errno")
private int code;
@Expose
@SerializedName("err")
private String msg;
private int total;
@Expose
@SerializedName("rsm")
private T content; public Result(T result) {
this.content = result;
} public Result(int code, T result) {
this.code = code;
this.content = result;
} public long getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public long getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public long getAllDataCount() {
return allDataCount;
} public void setAllDataCount(int allDataCount) {
this.allDataCount = allDataCount;
} public long getNextPage() {
return nextPage;
} public void setNextPage(int nextPage) {
this.nextPage = nextPage;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public int getTotal() {
return total;
} public void setTotal(int total) {
this.total = total;
} public T getContent() {
return content;
} public void setContent(T content) {
this.content = content;
}
}
Android用Gson解析JSON字符串的更多相关文章
- 【Android进阶】Gson解析json字符串的简单应用
在客户端与服务器之间进行数据传输,一般采用两种数据格式,一种是xml,一种是json.这两种数据交换形式各有千秋,比如使用json数据格式,数据量会比较小,传输速度快,放便解析,而采用xml数据格式, ...
- Gson解析json字符串、json数组转换成对象
实体类: public class Product { private int id; private String name; private String date; public int get ...
- Android 使用Gson解析json案例具体解释
一.眼下解析json有三种工具:org.json(Java经常使用的解析),fastjson(阿里巴巴project师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:h ...
- Gson解析json字符串
// 解析传递过来的json字符串 JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(strJson).g ...
- Android中使用Gson解析JSON数据的两种方法
Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下 Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...
- Android网络之数据解析----使用Google Gson解析Json数据
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Android中使用Gson解析JSON数据
Android中使用Gson解析JSON数据 在Android中可以使用Gson解析JSON数据 首先,从 code.google.com/p/google-gson/downloads/list ...
- 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题
1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...
随机推荐
- c#基础-自动内存管理
1.自动垃圾回收是什么? 在非托管环境下程序员要自已管理内存,由疏忽的原因,通常会犯两种错误,请求内存后在不使用时忘记释放,或使用已经释放了的内存.但在托管环境下,程序员不用担心这两个问题,C ...
- 使用AutoIT检测已安装软件,并将结果保存在桌面
$file = "\adobe安装列表.txt" $regedit1 = "hklm64\SOFTWARE\Wow6432Node\Microsoft\Windows\C ...
- C++中的内存区[译文]
C++ 中的内存区 Const Data: The const data area stores string literals and other data whose values are kno ...
- COS-7设备管理
操作系统(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行. 操作系 ...
- LeetCode——Diameter of Binary Tree
LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...
- IE10下阿里旺旺无法快速登录解决办法
一直都是用Chrome浏览器的,如果已经登录了旺旺,打开淘宝登录的时候都是会有快速登录的,免得手工输入用户名密码了.不经意间用IE10打开淘宝,登录时发现无法使用快速登录,一番研究后发现,IE做了保护 ...
- AppWidget源码分析---updateAppWidget过程分析
转[原文] 前面一篇文章,分析了AppWidgetProvider和RemoteView的源码,从中我们可以知道它们的实现原理,AppWidgetProvider是一个BroadcastReceive ...
- Codeforces Round #363 (Div. 2) A、B、C
A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- jsonp跨域传过来的数据格式
var uri1 = 'https://api.github.com?callback=JSON_CALLBACK'; /**/JSON_CALLBACK({ "meta": { ...
- MVVM架构说明1
MVVM是Model-View-ViewModel的简写.微软的WPF带来了新的技术体验,如Sliverlight.音频.视频.3D.动画……,这导致了软件UI层更加细节化.可定制化.同时,在技术层面 ...