设为首页 加入收藏

TOP

Json解析与Gson解析(一)
2017-10-11 15:31:46 】 浏览:2306
Tags: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 : 10000
45          * title : 新闻
46          * type : 1
47          * des : 这是一条有内涵的新闻1111
48          */
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         @Override
88         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.
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇获取android的SDK或者手机目录路径 下一篇最合适UX设计师的五款原型工具

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目