你的位置:首页 > 操作系统

[操作系统]Json解析与Gson解析

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

 ①json解析

 1 /** 2   * 普通的json解析 3   * @param s 4   * @throws JSONException 5  */ 6  private void jsonJieXi(String s) throws JSONException { 7   //创建json对象 8   JSONObject jsonObject1 = new JSONObject(s); 9   String retcode = jsonObject1.getString("retcode");10   String header = jsonObject1.getString("header");11   Log.i(TAG, "retcode=" + retcode + "----------header=" + header);12 13   JSONArray data = jsonObject1.getJSONArray("data");14 15   for (int i = 0; i < data.length(); i++) {16    JSONObject obj = (JSONObject) data.get(i);17    String ids = (String) obj.get("id");18    String title = (String) obj.get("title");19    String type = (String) obj.get("type");20    String des = (String) obj.get("des");21    Log.i(TAG, "ids=" + ids + "--title=" + title + "--type=" + type + "--des=" + des + "\n");22   }23  }

  ②gson解析

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

 

  

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

  

 

  3)添加gson的依赖包

  

 

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

  

 1 import java.util.List; 2  3 /** 4  * 作者:AdminHeJun. 5  * 时间:2017/9/3 19:28. 6  * 邮箱:1270960250@qq.com 7  * 内容: 8  * 修改: 9 */10 11 public class NewsInfo {12 13 14  private int retcode;15  private String header;16  private List<DataBean> data;17 18  public int getRetcode() {19   return retcode;20  }21 22  public void setRetcode(int retcode) {23   this.retcode = retcode;24  }25 26  public String getHeader() {27   return header;28  }29 30  public void setHeader(String header) {31   this.header = header;32  }33 34  public List<DataBean> getData() {35   return data;36  }37 38  public void setData(List<DataBean> data) {39   this.data = data;40  }41 42  public static class DataBean {43   /**44    * id : 1000045    * title : 新闻46    * type : 147    * des : 这是一条有内涵的新闻111148   */49 50   private int id;51   private String title;52   private int type;53   private String des;54 55   public int getId() {56    return id;57   }58 59   public void setId(int id) {60    this.id = id;61   }62 63   public String getTitle() {64    return title;65   }66 67   public void setTitle(String title) {68    this.title = title;69   }70 71   public int getType() {72    return type;73   }74 75   public void setType(int type) {76    this.type = type;77   }78 79   public String getDes() {80    return des;81   }82 83   public void setDes(String des) {84    this.des = des;85   }86 87   @Override88   public String toString() {89    return "DataBean{" +90      "id=" + id +91      ", title='" + title + '\'' +92      ", type=" + type +93      ", des='" + des + '\'' +94      '}';95   }96  }97 98 }

 

 

 

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

  

 1 /** 2   * gson解析json数据 3   * 4   * @param s 5  */ 6  private void gsonUtil(String s) { 7   //创建一个gson对象 8   Gson gson = new Gson(); 9   //解析json数据10   NewsInfo newsInfo = gson.fromJson(s, NewsInfo.class);11 12   String header = newsInfo.getHeader();13   int retcode = newsInfo.getRetcode();14 15   Log.i(TAG, "retcode=" + retcode + "----------header=" + header);16   17   //得到data数据的集合18   List<NewsInfo.DataBean> data = newsInfo.getData();19 20   Log.i(TAG, "data------->" + data.toString());21  }

打印结果

1 retcode=200----------header=http://192.168.126.26:8080/news/a.jpg2 3 4 5 data------->[DataBean{id=10000, title='新闻', type=1, des='这是一条有内涵的新闻1111'},
DataBean{id=10002, title='专题', type=10, des='这是一条有内涵的新闻222222'},
DataBean{id=10003, title='组图2', type=2, des='这是一条有内涵的新闻333333'},
DataBean{id=10006, title='组图4', type=2, des='这是一条有内涵的新闻333333'},
DataBean{id=10008, title='组图5', type=2, des='这是一条有内涵的新闻333333'},
DataBean{id=10003, title='组图6', type=2, des='这是一条有内涵的新闻ddddd33'},
DataBean{id=10003, title='组图7', type=2, des='这是一条有内涵的新闻3ssss33333'},
DataBean{id=10003, title='组图8', type=2, des='这是一条有内涵的新闻33dddd33333'},
DataBean{id=10004, title='互动', type=3, des='这是一条有内涵的新闻444444'}]

最后贴上原始的json数据

 1 { 2  "retcode": 200, 3  "data": [ 4   { 5    "id": 10000, 6    "title": "新闻", 7    "type": 1, 8   "des":"这是一条有内涵的新闻1111"   9   },10   {11    "id": 10002,12    "title": "专题",13    "type": 10,14    "des":"这是一条有内涵的新闻222222" 15   },16   {17    "id": 10003,18    "title": "组图2",19    "type": 2,20    "des":"这是一条有内涵的新闻333333" 21   },22   {23    "id": 10006,24    "title": "组图4",25    "type": 2,26    "des":"这是一条有内涵的新闻333333" 27   },28   {29    "id": 10008,30    "title": "组图5",31    "type": 2,32    "des":"这是一条有内涵的新闻333333" 33   },34   {35    "id": 10003,36    "title": "组图6",37    "type": 2,38    "des":"这是一条有内涵的新闻ddddd33" 39   },40   {41    "id": 10003,42    "title": "组图7",43    "type": 2,44    "des":"这是一条有内涵的新闻3ssss33333" 45   },46   {47    "id": 10003,48    "title": "组图8",49    "type": 2,50    "des":"这是一条有内涵的新闻33dddd33333" 51   },52   {53    "id": 10004,54    "title": "互动",55    "type": 3,56    "des":"这是一条有内涵的新闻444444" 57   }58  ],59  "header":""60  61  62 }

好啦操作到此结束