使用Gson解析json数据
2016-07-27 00:47
471 查看
使用Gson解析json数据,非常方便,要细心的配置实体类Model。
比如json字符串为:
{ "showapi_res_code": 0, "showapi_res_error": "", "showapi_res_body": { "allPages": 1503, "ret_code": 0, "contentlist": [ { "id": "579758166e366c56d29064f5", "text": "寒冬的早晨,你在池中奋力地划水,蛙泳、仰泳、蝶泳、自由泳,还有令人折服倾倒的潜泳!岸上的老汉急了:“你这坏小子!你喝干了粪池子,不让俺种地了!", "title": "搞笑的恶&心的整人的短信", "type": 1, "ct": "2016-07-26 20:31:18.023" }, { "id": "579758166e366c56d29064f4", "text": "我昨晚做了个梦,梦见太白金星告诉我很多天机!原来咱俩五百年前都是神仙。我哭了,我都想起来了!啸天,我是二郎啊!你还记得我么,啸天!这几百年还好吗?", "title": "经典短信42个,逗乐你!", "type": 1, "ct": "2016-07-26 20:31:18.023" } ], "currentPage": 1, "allNum": 30042, "maxResult": 20 } }```
照上面的json数据,Model实体类为:
public class joke {
private String showapi_res_code; private String showapi_res_error; private body showapi_res_body; public static class body{ private String allPages; private String ret_code; private String currentPage; private String allNum; private String maxResult; private List<jokemodel> contentlist; public String getAllPages() { return allPages; } public void setAllPages(String allPages) { this.allPages = allPages; } public String getRet_code() { return ret_code; } public void setRet_code(String ret_code) { this.ret_code = ret_code; } public String getCurrentPage() { return currentPage; } public void setCurrentPage(String currentPage) { this.currentPage = currentPage; } public String getAllNum() { return allNum; } public void setAllNum(String allNum) { this.allNum = allNum; } public String getMaxResult() { return maxResult; } public void setMaxResult(String maxResult) { this.maxResult = maxResult; } public List<jokemodel> getContentlist() { return contentlist; } public void setContentlist(List<jokemodel> contentlist) { this.contentlist = contentlist; } public static class jokemodel{ private String id; private String text; private String title; private int type; private String ct; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getCt() { return ct; } public void setCt(String ct) { this.ct = ct; } } } public String getShowapi_res_code() { return showapi_res_code; } public void setShowapi_res_code(String showapi_res_code) { this.showapi_res_code = showapi_res_code; } public String getShowapi_res_error() { return showapi_res_error; } public void setShowapi_res_error(String showapi_res_error) { this.showapi_res_error = showapi_res_error; } public body getShowapi_res_body() { return showapi_res_body; } public void setShowapi_res_body(body showapi_res_body) { this.showapi_res_body = showapi_res_body; }
}
注意实体类中的子类必须是static静态类修饰。字段名称必须与json中的键的名称一致。 在调用时使用下面的方法:
public void Gson(String json){
Gson gson = new Gson();
Type type = new TypeToken(){}.getType();
joke j = gson.fromJson(json,type);
for (int i = 0; i < j.getShowapi_res_body().getContentlist().size(); i++) {
Log.i(“setOnClickListener”,”Title:”+j.getShowapi_res_body().getContentlist().get(i).getTitle()+”,Content:”+j.getShowapi_res_body().getContentlist().get(i).getText());
}
}
“`
github源码地址如下:
https://github.com/fsdsds/GsonDemo
相关文章推荐
- 我是运营,我没有假期
- 使用 Python 处理 JSON 格式的数据
- Gson.toJson()时内存溢出StackOverflowError
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- newtonsoft.json解析天气数据出错解决方法
- 修复mysql数据库
- 浅析SQL数据操作语句
- SQLServer 数据导入导出的几种方法小结
- 简述MySQL分片中快速数据迁移
- MySQL数据备份之mysqldump的使用详解
- C#实现窗体间传递数据实例
- vbs 解析json jsonp的方法
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- C#中的委托数据类型简介
- SQL Server删除表及删除表中数据的方法
- SqlServer2008误操作数据(delete或者update)后恢复数据的方法