Android Gson使用入门及GsonFormat插件的使用
Gson 是 Google 官方提供的用来在 Java 对象和 JSON 之间进行互相转换的Java类库。 
我之前在使用Eclipse开发Android的时候,并没有经常使用Gson,而是使用原生的JSONObject解析,最近转用Android Studio,配合该IDE里面强大的插件,使用Gson很大程度地提高了开发效率。本篇博客将介绍Gson的基本使用方法,配合实际案例体会Gson带来的便捷开发体验。
1、待解析的Json数据
{
  "code": 0,
  "msg": "轮播会议获取成功",
  "records": [
    {
      "joinNumber": 3,
      "id": 10,
      "startDateStr": "2016-10-26 09:00 星期三",
      "theme": "2016年度海外高层次人群聚会"
    },
    {
      "joinNumber": 3,
      "id": 1,
      "startDateStr": "2016-10-24 08:00 星期一",
      "theme": "2016年度苏州医疗会议"
    }
  ]
}
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 
2、创建对应的JavaBean:MeetingData.java
package com.leohan.gsondemo;
import java.util.List;
/**
 * Created by Leo on 16/3/14.
 */
public class MeetingData {
    /**
     * code : 0
     * msg : 轮播会议获取成功
     * records : [{"joinNumber":3,"id":10,"startDateStr":"2016-10-26 09:00 星期三","theme":"2016年度海外高层次人群聚会"},{"joinNumber":3,"id":1,"startDateStr":"2016-10-24 08:00 星期一","theme":"2016年度苏州医疗会议"}]
     */
    private int code;
    private String msg;
    /**
     * joinNumber : 3
     * id : 10
     * startDateStr : 2016-10-26 09:00 星期三
     * theme : 2016年度海外高层次人群聚会
     */
    private List<RecordsEntity> records;
    public void setCode(int code) {
        this.code = code;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public void setRecords(List<RecordsEntity> records) {
        this.records = records;
    }
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
    public List<RecordsEntity> getRecords() {
        return records;
    }
    public static class RecordsEntity {
        private int joinNumber;
        private int id;
        private String startDateStr;
        private String theme;
        public void setJoinNumber(int joinNumber) {
            this.joinNumber = joinNumber;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setStartDateStr(String startDateStr) {
            this.startDateStr = startDateStr;
        }
        public void setTheme(String theme) {
            this.theme = theme;
        }
        public int getJoinNumber() {
            return joinNumber;
        }
        public int getId() {
            return id;
        }
        public String getStartDateStr() {
            return startDateStr;
        }
        public String getTheme() {
            return theme;
        }
    }
}
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 
首先分析一下要解析的Json数据,它包含了一个字段名为”records”的JsonArray。  
由此,我们生成该Json数据对应的JavaBean,并在里面创建了records数组对应的内部类RecordsEntity。
3、使用Gson解析Json数据
 Gson gson = new Gson();
        MeetingData meetingData = gson.fromJson(jsonStr, MeetingData.class);
        List<MeetingData.RecordsEntity> records = meetingData.getRecords();
- 1
 - 2
 - 3
 
- 1
 - 2
 - 3
 
运行程序,打印records对象:  
至此就已经完成了Json对象的解析。Gson的其它用法,如处理泛型等就不展开阐述了。
4、使用GsonFormat插件加速开发 
在第二步里,新建了一个JavaBean对应于待解析的Json数据。有没有直接辅助生成JavaBean的插件呢?Introducing GsonFormat…
那么,GsonFormat插件如何使用呢?
- Preferences –> plugins –>搜索GsonFormat安装 
 - 安装完以后新建一个JavaBean,如图所示操作: 
 - 在弹出的界面中填入要解析的Json数据 
点击OK,GsonFormat就可以自动帮我们创建好这个JavaBean了。 
                    
                
                
            
        
浙公网安备 33010602011771号