在volley框架中有一个 protected Response<Result<T>> parseNetworkResponse(NetworkResponse response){}函数。从服务器上或者在缓存中获取的JSON字符串在这个函数进行解析。

  1. String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, HTTP.UTF_8))
  2. Result<T> result = GsonUtil.getResult(jsonString, mTypeToken);

我们可以定义一个GsonUtil类。里面写一些Gson和JSON相互转化的功能函数。例如上用到的getResult函数。利用该函数获取的Result<T>这个JavaBean。也就是返回的规范,该规范由前端开发人员和后台开发人员商量得出。既然得到的JavaBean,那么我再要获取JSON里面的数据就像从一个类中获取一个数据一样简单了。

  1. public static <T> T getResult(String jsonData, TypeToken<T> type) {
  2. Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
  3. return (T) gson.fromJson(jsonData, type.getType());
  4. }

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} )

  1. package whu.cn.whushare.bean;
  2.  
  3. import com.google.gson.annotations.Expose;
  4. import com.google.gson.annotations.SerializedName;
  5.  
  6. public class Result<T> {
  7. @Expose
  8. @SerializedName("currentPage")
  9. private long currentPage;
  10. @Expose
  11. @SerializedName("pageSize")
  12. private long pageSize;
  13. @Expose
  14. @SerializedName("allDataCount")
  15. private long allDataCount;
  16. @Expose
  17. @SerializedName("nextPage")
  18. private long nextPage;
  19. @Expose
  20. @SerializedName("errno")
  21. private int code;
  22. @Expose
  23. @SerializedName("err")
  24. private String msg;
  25. private int total;
  26. @Expose
  27. @SerializedName("rsm")
  28. private T content;
  29.  
  30. public Result(T result) {
  31. this.content = result;
  32. }
  33.  
  34. public Result(int code, T result) {
  35. this.code = code;
  36. this.content = result;
  37. }
  38.  
  39. public long getCurrentPage() {
  40. return currentPage;
  41. }
  42.  
  43. public void setCurrentPage(int currentPage) {
  44. this.currentPage = currentPage;
  45. }
  46.  
  47. public long getPageSize() {
  48. return pageSize;
  49. }
  50.  
  51. public void setPageSize(int pageSize) {
  52. this.pageSize = pageSize;
  53. }
  54.  
  55. public long getAllDataCount() {
  56. return allDataCount;
  57. }
  58.  
  59. public void setAllDataCount(int allDataCount) {
  60. this.allDataCount = allDataCount;
  61. }
  62.  
  63. public long getNextPage() {
  64. return nextPage;
  65. }
  66.  
  67. public void setNextPage(int nextPage) {
  68. this.nextPage = nextPage;
  69. }
  70.  
  71. public int getCode() {
  72. return code;
  73. }
  74.  
  75. public void setCode(int code) {
  76. this.code = code;
  77. }
  78.  
  79. public String getMsg() {
  80. return msg;
  81. }
  82.  
  83. public void setMsg(String msg) {
  84. this.msg = msg;
  85. }
  86.  
  87. public int getTotal() {
  88. return total;
  89. }
  90.  
  91. public void setTotal(int total) {
  92. this.total = total;
  93. }
  94.  
  95. public T getContent() {
  96. return content;
  97. }
  98.  
  99. public void setContent(T content) {
  100. this.content = content;
  101. }
  102. }

Android用Gson解析JSON字符串的更多相关文章

  1. 【Android进阶】Gson解析json字符串的简单应用

    在客户端与服务器之间进行数据传输,一般采用两种数据格式,一种是xml,一种是json.这两种数据交换形式各有千秋,比如使用json数据格式,数据量会比较小,传输速度快,放便解析,而采用xml数据格式, ...

  2. Gson解析json字符串、json数组转换成对象

    实体类: public class Product { private int id; private String name; private String date; public int get ...

  3. Android 使用Gson解析json案例具体解释

    一.眼下解析json有三种工具:org.json(Java经常使用的解析),fastjson(阿里巴巴project师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:h ...

  4. Gson解析json字符串

    // 解析传递过来的json字符串 JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(strJson).g ...

  5. Android中使用Gson解析JSON数据的两种方法

    Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下   Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...

  6. Android网络之数据解析----使用Google Gson解析Json数据

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. Android中使用Gson解析JSON数据

      Android中使用Gson解析JSON数据 在Android中可以使用Gson解析JSON数据 首先,从 code.google.com/p/google-gson/downloads/list ...

  8. 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题

    1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...

  9. 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址

    在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...

随机推荐

  1. 安装配置资产管理软件GLPI

    GLPI是法语Gestionnaire libre de parc informatique的缩写,是一款历史悠久的资产管理软件: GLPI提供功能全面的IT资源管理接口,可以用来建立数据库全面管理I ...

  2. 利用canvas和RGraph作图

    利用canvas可以直接在页面中绘制各种复杂的图形,其中引用到一个Rgraph的插件. Rgraph插件使用非常方便,只需几步就可以完成一个折线图.饼图.柱状图,或是其中两者图形的结合! (1) 引用 ...

  3. java实现定时任务的三种方法 - 转载

    java实现定时任务的三种方法 /** * 普通thread * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着, * 通过sleep方法来达到定时任务的效果.这样可以快速简 ...

  4. Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。

    问题描述:寻找反转序列中最小的元素. 算法分析:和寻找某个数是一个道理,还是利用二分查找,总体上分两种情况.nums[left]<=nums[mid],else.但是,在截取子序列的时候,有可能 ...

  5. ZC_RemoteThread

    1.Z_WinMain.cpp #include <windows.h> #include "resource.h" #include "Z_RemoteFu ...

  6. d2.js学习笔记(七)——动态SVG坐标空间

    目标 在这一章,我们将学习如何使SVG坐标空间是动态的,这样我们的数据可视化不论数据是什么,都始终是可见的. 我们会使得SVG坐标空间尺度上调或下调来适于我们的数据. 三个SVG长方形 我们就从三个长 ...

  7. js中的数组对象中的方法解析

    concat()方法:  合并两个数组,返回新对象的结果: join()方法 :  把数组内的所有元素加入到一个字符串中,传入的分隔符就是指定的分隔符 pop()方法: 删除数组并返回数组的最后一个元 ...

  8. 【LABVIEW到C#】3》String的操作之Match Pattern Funtion.vi

    C#实现如下 using System; using System.Collections.Generic; using System.Text; using System.Text.RegularE ...

  9. hdu2586倍增lca

    求距离 #include<map> #include<set> #include<cmath> #include<queue> #include< ...

  10. 创建CMD启动环境

    我们可以用一个cmd文件,通过doskey命令模拟linux下的alias,指定一些我们习惯的命令名,比如: env.cmd @echo off doskey alias=doskey /macros ...