GSON
{
"data": [
{
"children": [
{
"id": 10007,
"title": "北京",
"type": 1,
"url": "/10007/list_1.json"
},
{
"id": 10006,
"title": "中国",
"type": 1,
"url": "/10006/list_1.json"
},
{
"id": 10008,
"title": "国际",
"type": 1,
"url": "/10008/list_1.json"
},
{
"id": 10010,
"title": "体育",
"type": 1,
"url": "/10010/list_1.json"
},
{
"id": 10091,
"title": "生活",
"type": 1,
"url": "/10091/list_1.json"
},
{
"id": 10012,
"title": "旅游",
"type": 1,
"url": "/10012/list_1.json"
},
{
"id": 10095,
"title": "科技",
"type": 1,
"url": "/10095/list_1.json"
},
{
"id": 10009,
"title": "军事",
"type": 1,
"url": "/10009/list_1.json"
},
{
"id": 10093,
"title": "时尚",
"type": 1,
"url": "/10093/list_1.json"
},
{
"id": 10011,
"title": "财经",
"type": 1,
"url": "/10011/list_1.json"
},
{
"id": 10094,
"title": "育儿",
"type": 1,
"url": "/10094/list_1.json"
},
{
"id": 10105,
"title": "汽车",
"type": 1,
"url": "/10105/list_1.json"
}
],
"id": 10000,
"title": "新闻",
"type": 1
},
{
"id": 10002,
"title": "专题",
"type": 10,
"url": "/10006/list_1.json",
"url1": "/10007/list1_1.json"
},
{
"id": 10003,
"title": "组图",
"type": 2,
"url": "/10008/list_1.json"
},
{
"dayurl": "",
"excurl": "",
"id": 10004,
"title": "互动",
"type": 3,
"weekurl": ""
}
],
"extend": [
10007,
10006,
10008,
10014,
10012,
10091,
10009,
10010,
10095
],
"retcode": 200
}
JSON数据如上 现在用Gson对上面的复杂数据进行解析 先建立JAVABEAN对象 来封装:
gson对象封装原则:
* 遇到{}就是一个对象
* 遇到[]就是一个Arraylist
* 对象中所有属性命名必须和服务器返回字段完全一致
所以上面的been应该这么写:
public class NewsMenuData {
public int retcode;
public ArrayList<String> extend;
public ArrayList<NewsData> data;
public class NewsData {
public String id;
public String title;
public int type;
public ArrayList<NewsTabData> children;
@Override
public String toString() {
return "NewsData [title=" + title + ", children=" + children + "]";
}
}
// 页签信息封装
public class NewsTabData {
public String id;
public String title;
public String url;
public int type;
@Override
public String toString() {
return "NewsTabData [title=" + title + "]";
}
}
@Override
public String toString() {
return "NewsMenuData [data=" + data + "]";
}
}
请求数据:用的xutils框架.
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, Constants.CATEGORIES_URL,
new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// 请求成功
String result = responseInfo.result;// 获取json字符串
// System.out.println("result:" + result);
processResult(result);
// 写缓存
CacheUtils.setCache(Constants.CATEGORIES_URL, result,
mActivity);
}
@Override
public void onFailure(HttpException error, String msg) {
// 请求失败
error.printStackTrace();
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
}
});
}
Gson解析的方法
protected void processResult(String result) {
// gson->json
Gson gson = new Gson();
mNewsMenuData = gson.fromJson(result, NewsMenuData.class);
System.out.println("解析结果:" + mNewsMenuData);
}
GSON的更多相关文章
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- Gson将字符串转换成JsonObject和JsonArray
以下均利用Gson来处理: 1.将bean转换成Json字符串: public static String beanToJSONString(Object bean) { return new Gso ...
- Gson解析纯Json数组
[ { "type": "123", "value": 123 }, { "type": "234" ...
- 【Gson】互相转化
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. 对象转为字符串 Strin ...
- Android Gson解析
目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:https://co ...
- Android总结之json解析(FastJson Gson 对比)
前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...
- gson笔记 解析json数据
gson中负责json数据解析的类是JsonReader. Json格式有两种结构,一种是对象(键值对的组合,无序),另外一种是数组(值的有序集合). 因此针对这两种格式,JsonReader提供了不 ...
- Android Gson的使用总结
1.概念 Gson是谷歌发布的一个json解析框架 2.如何获取 github:https://github.com/google/gson android studio使用 compile 'com ...
- Android JSON、GSON、FastJson的封装与解析
声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...
- Java Gson 简要笔记
Gson是Google开发的Java比较好用的 Json工具. 使用挺简单,假设有个类: class Runner { int attr; String name; public Runner(int ...
随机推荐
- WPF+Caliburn.Micro 杂记
开发过程中的小问题总结 1DataGrid的Header里面给Checkbox绑定IsEnabled,绑不上去. 2由A页面跳转到B页面,再由B页面返回一个值 3DataGrid里面的行通过一个方法 ...
- iOS之UI--UITabBarController
前言:跟UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是QQ.微信等应用. UITabBarCo ...
- 二维码扫描利用ZBar实现
上次是根据系统的属性自己封装的一个二维码扫描,这次给大家介绍一下基于ZBar集成的类似于QQ二维码扫描界面的二维码扫描的效果. ...
- CTF中那些脑洞大开的编码和加密
0x00 前言 正文开始之前先闲扯几句吧,玩CTF的小伙伴也许会遇到类似这样的问题:表哥,你知道这是什么加密吗?其实CTF中脑洞密码题(非现代加密方式)一般都是各种古典密码的变形,一般出题者会对密文进 ...
- opencms忘记Admin用户登录密码解决方案
不知道现在还有多少人在用opencms: 我今天就是遇到一个问题,很久之前部署的一台opencms服务器,突然忘记密码了,记录下处理方法: 希望对你有帮助: opencms忘记Admin用户登录密码了 ...
- Effective Java 17 Design and document for inheritance or else prohibit it
Principles The class must document its self-use of overridable methods. A class may have to provide ...
- Log4j配置与使用
log4j是Java社区事实上的日志标准解决方案.使用起来比较简单. 一. 简单使用 1.下载jar包放到lib文件夹,并加入到build path中: 2.编写log4j.properties文件, ...
- cocos2d-x之首选项数据初试
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...
- hibernate基本类型映射
model包: Book.java: package model; import java.sql.Blob;import java.util.Date; public class Book { ...
- 复制文件的bat脚本
数据库备份到不同的机器上的一段脚本 使用系统的copy 命令来复制单个文件 如果要复制某个文件夹,则使用xcopy 命令 set date=%Date:~0,4%%Date:~5,2%%Date:~8 ...