博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gson解析json数据
阅读量:6230 次
发布时间:2019-06-21

本文共 5336 字,大约阅读时间需要 17 分钟。

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
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
getList() { return list; } public void setList(List
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
matchs = new ArrayList
(); matchs = new Gson().fromJson(NBAMatchJsons,new TypeToken
>() {}.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
citys = new Gson().fromJson(jsonStr, new TypeToken
>() {}.getType()); System.out.println(citys.size());  }}

 

转载地址:http://bwxna.baihongyu.com/

你可能感兴趣的文章
蒲公英分布平台下载更新实现
查看>>
Mysql常用命令详解
查看>>
依赖注入的方式
查看>>
从VBA到Delphi
查看>>
将父类activity context传递给fragment
查看>>
eclipse中导入SVN项目步骤
查看>>
2018-2019-1 20165226 《信息安全系统设计基础》第4周学习总结
查看>>
Oracle之自动收集统计信息
查看>>
互联网技术部门该如何管理
查看>>
微软2014实习生及秋令营技术类职位在线测试-题目3 : Reduce inversion count
查看>>
WebView兼容问题分析报告
查看>>
主机字节序
查看>>
【TYVJ 1056】能量项链
查看>>
手把手教你开发Windows Phone 8应用程序(序)
查看>>
hzk16在项目中的使用
查看>>
pl/sql编程(九)
查看>>
mvn项目中的pom文件提示Error parsing lifecycle processing instructions解决
查看>>
[转] react-router4 + webpack Code Splitting
查看>>
C# 集合 — Hashtable 线程安全
查看>>
Python 第一天
查看>>