java android使用Gson解析泛型json数据
那就直接开始吧。
在我们获取服务器返回的json数据有时候会出现这种情况,比如:
{"body":{"attrName":"feed","result":[{"time":63000000,"food":14,"id":2,}]},"when":"20180426170357+0800"}
{"body":{"attrName":"media","result":{"singer":"薛之谦","name":"你还要我怎样"}},"when":"20180426170357+0800"}
两条数据其他结构相同,但前者的"result"是一个json数组,而后者直接是一条json字符串。
此时我们可以将实体类这样写:
public class ReportBean<T>{
private Body<T> body;
private String when;
public Body<T> getBody() {
return body;
}
public void setBody(Body<T> body) {
this.body= body;
}
public String getWhen() {
return when;
}
public void setWhen(String when) {
this.when = when;
}
public class Body<T> {
private String attrName;
private T result;
public String getAttrName() {
return attrName;
}
public void setAttrName(String attrName) {
this.attrName = attrName;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
}
}
接下来,当我们处理数据时——
第一种,json数组:
Type jsonType = new TypeToken<ReportBean<List<ResultBean>>>() {}.getType();
ReportBean<List<ResultBean>> reportBean = new Gson().fromJson(jsonStr, jsonType);
要获取的泛型数组即bean.getBody().getResult;
第二种,json字符串:
Type jsonType = new TypeToken<ReportBean<ResultBean2>>() {}.getType();
ReportBean<ResultBean2> reportBean = new Gson().fromJson(jsonStr, jsonType);
ResultBean即为自定义的泛型中具体的数据实体类,此时数据就都已经解析到 reportBean 里面了,再根据自己的需要取出即可。
java android使用Gson解析泛型json数据的更多相关文章
- Gson解析复杂Json数据
背景 json是一种数据格式,便于数据传输.存储.交换. gson是 ...
- Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据
版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...
- android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...
- (转)android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection 复制代码 ...
- android客户端从服务器端获取json数据并解析的实现代码(重要)
首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...
- 解析天气预报JSON数据
解析天气预报JSON数据 JSON字符串 constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'"status" ...
- mormot解析天气预报JSON数据
mormot解析天气预报JSON数据 uses SynCommons; constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'&qu ...
- Android解析服务器Json数据实例
Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...
- Android利用Gson解析嵌套多层的Json
参考:http://www.cnblogs.com/jxgxy/p/3677256.html 比如我们要解析一个下面这种的Json: String json = {"a":&quo ...
随机推荐
- Linux创建系统用户
#!/bin/bash users_home_front_dir="/data/users/" ssh_user=$1 user_group=$2 server_user_path ...
- Spring Security教程(三):自定义表结构
在上一篇博客中讲解了用Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项 ...
- IReport5.6.0创建数据库连接找不到驱动(iReport中ClassNotFoundError错误的解决)
情景:iRoport中选择com.microsoft.jdbc.sqlserver.SQLServerDriver的JDBC Driver;连接时出现ClassNotFoundError错误 当见到下 ...
- how-apache-flink-enables-new-streaming-applications-part-1
https://data-artisans.com/blog/how-apache-flink-enables-new-streaming-applications-part-1 http://www ...
- Ubantu 使用extundelete恢复数据
所以在维护系统的时候,要慎之又慎,但是有时难免会出现数据被误删除的情况,在这个时候改如何快速.有效地恢复数据呢?本文我们就来介绍一下Linux系统下常用的几个数据恢复工具. 一.如何使用“rm -rf ...
- 每日英语:The best time for coffee is 10:30 a.m.
For many of us, it is the rocket fuel that gets us going first thing in the morning. But one expert ...
- yum卸载失败Error in PREUN scriptlet in rpm package postgresql-server
yum --setopt=tsflags=noscripts remove 参考 https://serverfault.com/questions/613256/yum-error-in-preun ...
- 面试总结:QuickSort 解析
Quick Sort http://en.wikipedia.org/wiki/Quicksort Quicksort, or partition-exchange sort, is a sortin ...
- 【word2vec】Distributed Representation——词向量
Distributed Representation 这种表示,它最早是 Hinton 于 1986 年提出的,可以克服 one-hot representation 的缺点. 其基本想法是: 通过训 ...
- 使用一层神经网络训练mnist数据集
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...