Gson解析json数据

1、解析单个对象

  JSON字符串如下

[
    {
        "formatDate":"2015-10-28",
        "date":"2015\u5e7410\u670828\u65e5 \u661f\u671f\u4e09",
        "list":[{
                    "sdate":"2015-10-28",
                    "time":"08:00",
                    "type":"\u5e38\u89c4\u8d5b",
                    "home_team":"\u8001\u9e70",
                    "home_score":"94",
                    "visit_team":"\u6d3b\u585e",
                    "visit_score":"106"
                },{
                    "sdate":"2015-10-28",
                    "time":"08:00",
                    "type":"\u5e38\u89c4\u8d5b",
                    "home_team":"\u516c\u725b",
                    "home_score":"97",
                    "visit_team":"\u9a91\u58eb",
                    "visit_score":"95"
                }]
    }
]

  封装解析的对象

public class NBAMatchs {
    /**
     * 时间
     */
    private String formatDate;
    /**
     * 赛程时间
     */
    private String date;
    /**
     * 赛事列表
     */
    List<NBAMatchBasic> list ;
    
    public String getFormatDate() {
        return formatDate;
    }
    public void setFormatDate(String formatDate) {
        this.formatDate = formatDate;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public List<NBAMatchBasic> getList() {
        return list;
    }
    public void setList(List<NBAMatchBasic> list) {
        this.list = list;
    }
}

  实体对象

public class NBAMatchBasic {

    private String sdate;
    
    private String time ;
    
    private String home_team;
    
    private String home_score;
    
    private String visit_team;
    
    private String visit_score;

    public String getSdate() {
        return sdate;
    }

    public void setSdate(String sdate) {
        this.sdate = sdate;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getHome_team() {
        return home_team;
    }

    public void setHome_team(String home_team) {
        this.home_team = home_team;
    }

    public String getHome_score() {
        return home_score;
    }

    public void setHome_score(String home_score) {
        this.home_score = home_score;
    }

    public String getVisit_team() {
        return visit_team;
    }

    public void setVisit_team(String visit_team) {
        this.visit_team = visit_team;
    }

    public String getVisit_score() {
        return visit_score;
    }

    public void setVisit_score(String visit_score) {
        this.visit_score = visit_score;
    }
}

  解析代码

    public static void main(String[] args) {
        String Json = "JSON字符串";
        NBAMatchs NBA = new NBAMatchs();
        NBA = new Gson().fromJson(Json, NBAMatchs.class);
        System.out.println(NBA.getFormatDate());
    }

 

2、解析List集合

  Json字符串如下

[
    {
        "formatDate":"2015-10-28",
        "date":"2015\u5e7410\u670828\u65e5 \u661f\u671f\u4e09",
        "list":[
                {
                    "sdate":"2015-10-28",
                    "time":"08:00",
                    "type":"\u5e38\u89c4\u8d5b",
                    "home_team":"\u8001\u9e70",
                    "home_score":"94",
                    "visit_team":"\u6d3b\u585e",
                    "visit_score":"106"
                },{
                    "sdate":"2015-10-28",
                    "time":"08:00",
                    "type":"\u5e38\u89c4\u8d5b",
                    "home_team":"\u516c\u725b",
                    "home_score":"97",
                    "visit_team":"\u9a91\u58eb",
                    "visit_score":"95"
                }]
    },
    {
        "formatDate":"2015-10-29",
        "date":"2015\u5e7410\u670829\u65e5 \u661f\u671f\u56db",
        "list":[
            {
                "sdate":"2015-10-29",
                "time":"07:00",
                "type":"\u5e38\u89c4\u8d5b",
                "home_team":"\u9b54\u672f",
                "home_score":"87",
                "visit_team":"\u5947\u624d",
                "visit_score":"88"
            },{
                "sdate":"2015-10-29",
                "time":"07:30",
                "type":"\u5e38\u89c4\u8d5b",
                "home_team":"\u51ef\u5c14\u7279\u4eba",
                "home_score":"112",
                "visit_team":"76\u4eba",
                "visit_score":"95"
            }]
    }
]

  解析代码

    public static void main(String[] args) {
        String NBAMatchJsons ="JSON字符串";
        List<NBAMatchs> matchs = new ArrayList<NBAMatchs>();
        matchs = new Gson().fromJson(NBAMatchJsons,new TypeToken<ArrayList<NBAMatchs>>() {}.getType());
        System.out.println(matchs.size());
    }

3、解析map集合

  Json字符串

{
    '1': 
        {'id': '1','code': 'bj','name': '北京','map': '39.90403, 116.40752599999996'},
    '2': 
        {'id': '2','code': 'sz','name': '深圳','map': '22.543099, 114.05786799999998'},
    '9': 
        {'id': '9','code': 'sh','name': '上海','map': '31.230393,121.473704'},
    '10':
         {'id': '10','code': 'gz','name': '广州','map': '23.129163,113.26443500000005'}
}

  解析测试类

public class Test {
  class City{  
	int id;  
	String name;  
	String code;  
	String map;  
}
  public static void main(String[] args) { String jsonStr="Json字符串"; Map<String, City> citys = new Gson().fromJson(jsonStr, new TypeToken<Map<String, City>>() {}.getType()); System.out.println(citys.size());   } }

 

posted @ 2016-06-07 10:40  星星满天  阅读(379)  评论(0编辑  收藏  举报