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 ...
随机推荐
- UIViewController的edgesForExtendedLayout属性
UIViewController的edgesForExtendedLayout属性 想必大家都遇到一种情况,明明y坐标设置的是0,但是总是被讨厌的导航栏给遮住.比如下面这个情况: UILabel *l ...
- android network develop(3)----Xml Parser
Normally, there are three type parser in android. Xmlpullparser, DOM & SAX. Google recomand Xmlp ...
- Effective Java 40 Design method signatures carefully
Principle Choose method names carefully. Don't go overboard in providing convenience methods. Avoid ...
- 比较全面的MySQL优化参考(上下篇)
转自:http://imysql.com/2015/05/24/mysql-optimization-reference-1.shtml 本文整理了一些MySQL的通用优化方法,做个简单的总结分享,旨 ...
- JS生成UUID的方法实例
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...
- Eclipse编译去除svn文件夹
使用Eclipse编译文件后,classes文件中总是有.svn的文件夹,这些文件没有什么用,而且影响build的速度 "Project->Properties->Java Bu ...
- Linux主流發行版本介紹
一.简介 而工欲善其事,必先利其器,Linux的世界相當廣大,除了最著名的Ubuntu以外還有不少發行版.然文人相輕,自古皆然,了解不同發行版的優勢不只嘴上攻防用的上,也是學Linux一個有趣的地方! ...
- Azure 上为Liunx VM 挂载File类型的存储。
1. Create a storage account in Azure, copy the storage account endpoint URL (with postfix of "f ...
- C++ new(1)
如果找工作的同学看一些面试的书,我相信都会遇到这样的题:sizeof 不是函数,然后举出一堆的理由来证明 sizeof 不是函数.在这里,和 sizeof 类似,new 和 delete 也不是函数, ...
- mybatis的#{}和${}的区别以及order by注入问题
前言略,直奔主题.. #{}相当于jdbc中的preparedstatement ${}是输出变量的值 你可能说不明所以,不要紧我们看2段代码: String sql = "select * ...