本文主要介绍json最原始的解析与google提供的gson工具类解析

 ①json解析

 /**
* 普通的json解析
* @param s
* @throws JSONException
*/
private void jsonJieXi(String s) throws JSONException {
//创建json对象
JSONObject jsonObject1 = new JSONObject(s);
String retcode = jsonObject1.getString("retcode");
String header = jsonObject1.getString("header");
Log.i(TAG, "retcode=" + retcode + "----------header=" + header); JSONArray data = jsonObject1.getJSONArray("data"); for (int i = ; i < data.length(); i++) {
JSONObject obj = (JSONObject) data.get(i);
String ids = (String) obj.get("id");
String title = (String) obj.get("title");
String type = (String) obj.get("type");
String des = (String) obj.get("des");
Log.i(TAG, "ids=" + ids + "--title=" + title + "--type=" + type + "--des=" + des + "\n");
}
}

  ②gson解析

  1)首先在AndroidStudio中安装一个GsonFormat插件

  

  2)新建一个javaben类然后按下组合键alt+insert                  把完整的json数据拷贝到编辑框中

  

  3)添加gson的依赖包

  

  4)然后生成Gson指定格式的java ben

  

 import java.util.List;

 /**
* 作者:AdminHeJun.
* 时间:2017/9/3 19:28.
* 邮箱:1270960250@qq.com
* 内容:
* 修改:
*/ public class NewsInfo { private int retcode;
private String header;
private List<DataBean> data; public int getRetcode() {
return retcode;
} public void setRetcode(int retcode) {
this.retcode = retcode;
} public String getHeader() {
return header;
} public void setHeader(String header) {
this.header = header;
} public List<DataBean> getData() {
return data;
} public void setData(List<DataBean> data) {
this.data = data;
} public static class DataBean {
/**
* id : 10000
* title : 新闻
* type : 1
* des : 这是一条有内涵的新闻1111
*/ private int id;
private String title;
private int type;
private String des; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} 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 getDes() {
return des;
} public void setDes(String des) {
this.des = des;
} @Override
public String toString() {
return "DataBean{" +
"id=" + id +
", title='" + title + '\'' +
", type=" + type +
", des='" + des + '\'' +
'}';
}
} }

  4)接下来就是使用gson解析啦

  

 /**
* gson解析json数据
*
* @param s
*/
private void gsonUtil(String s) {
//创建一个gson对象
Gson gson = new Gson();
//解析json数据
NewsInfo newsInfo = gson.fromJson(s, NewsInfo.class); String header = newsInfo.getHeader();
int retcode = newsInfo.getRetcode(); Log.i(TAG, "retcode=" + retcode + "----------header=" + header); //得到data数据的集合
List<NewsInfo.DataBean> data = newsInfo.getData(); Log.i(TAG, "data------->" + data.toString());
}

打印结果

 retcode=----------header=http://192.168.126.26:8080/news/a.jpg

 data------->[DataBean{id=, title='新闻', type=, des='这是一条有内涵的新闻1111'},
DataBean{id=, title='专题', type=, des='这是一条有内涵的新闻222222'},
DataBean{id=, title='组图2', type=, des='这是一条有内涵的新闻333333'},
DataBean{id=, title='组图4', type=, des='这是一条有内涵的新闻333333'},
DataBean{id=, title='组图5', type=, des='这是一条有内涵的新闻333333'},
DataBean{id=, title='组图6', type=, des='这是一条有内涵的新闻ddddd33'},
DataBean{id=, title='组图7', type=, des='这是一条有内涵的新闻3ssss33333'},
DataBean{id=, title='组图8', type=, des='这是一条有内涵的新闻33dddd33333'},
DataBean{id=, title='互动', type=, des='这是一条有内涵的新闻444444'}]

最后贴上原始的json数据

 {
"retcode": ,
"data": [
{
"id": ,
"title": "新闻",
"type": ,
"des":"这是一条有内涵的新闻1111"
},
{
"id": ,
"title": "专题",
"type": ,
"des":"这是一条有内涵的新闻222222"
},
{
"id": ,
"title": "组图2",
"type": ,
"des":"这是一条有内涵的新闻333333"
},
{
"id": ,
"title": "组图4",
"type": ,
"des":"这是一条有内涵的新闻333333"
},
{
"id": ,
"title": "组图5",
"type": ,
"des":"这是一条有内涵的新闻333333"
},
{
"id": ,
"title": "组图6",
"type": ,
"des":"这是一条有内涵的新闻ddddd33"
},
{
"id": ,
"title": "组图7",
"type": ,
"des":"这是一条有内涵的新闻3ssss33333"
},
{
"id": ,
"title": "组图8",
"type": ,
"des":"这是一条有内涵的新闻33dddd33333"
},
{
"id": ,
"title": "互动",
"type": ,
"des":"这是一条有内涵的新闻444444"
}
],
"header":"http://192.168.126.26:8080/news/a.jpg" }

好啦操作到此结束

最新文章

  1. T-SQL 如何获取一个表的列名
  2. 8. apache服务实验笔记
  3. Centos | Linux 下安装启动 mysql 出现 8618 [ERROR] Aborting,查看日志:Plugin &#39;FEDERATED&#39; is disabled.
  4. 4817 江哥的dp题d
  5. sqlplus 中spool命令的简单用法
  6. 数组拷贝 copyOf()
  7. sdutoj 2604 Thrall’s Dream
  8. ASP.NET 打包下载文件
  9. js方法在对象中的状态
  10. C#UDP广域网,局域网通信-原理分析
  11. 第48章:MongoDB-备份和恢复
  12. 【HANA系列】SAP HANA XS的JavaScript API详解
  13. Sudoku(第二次作业)
  14. Python三级菜单增删改查
  15. Tomcat 配置Https
  16. spring处理线程并发
  17. java入门学习(十四)运算语句for
  18. [转] handsontable的核心方法
  19. [转]Windows上搭建Kafka运行环境
  20. linux下一个网卡配置多个ip【虚拟ip】

热门文章

  1. Garden of Eden
  2. 清北学堂模拟赛d1t1 位运算1(bit)
  3. 开源的多行字符串工具: 在JS中整段地写HTML
  4. Holedox Eating HDU4302 模拟
  5. Stealing Harry Potter&#39;s Precious BFS+DFS
  6. 定义SAP Portal Url别名
  7. linux 设置root可以远程登陆
  8. IOS - UIView停止交互
  9. java集合: List、Set、Map总结 + HashMap/Hashtable 差别
  10. 在GNU Linux中怎样得到一个进程当前的流量