众所周知,我们在做新闻、天气预报等类型的app的时候,经常会碰到需要将获取的json网络数据进行数据解析,以方便我们获取需要的信息并进行封装显示,这时候我们有两种解析方式:原生解析和Gson框架解析;
接下来就这两种解析方式进行一个总结:
一:Gson框架解析
注意:在我们进行实体类封装时,我们需要获取的数据,如果有父类,我们也需要将父类封装起来,不然我们就获取不到子类即我们需要的数据信息;
封装的实体类,必须和json数据的key名字完全一致,否则无法解析;
Gson框架的下载地址我就不贴出来了,这里只要讲解两种不同类型的json数据的实体封装及利用框架进行数据解析;
请注意一下两种json数据的不同:
第一种:
{
"reason": "成功的返回",
"result": {
"stat": "1",
"data": [
{
"title": "男有陈冠希,女有张静初,专业睡导演30年惨遭导演太太集体封杀",
"date": "2016-09-12 06:18",
"author_name": "分合晚歌",
"thumbnail_pic_s": "http://07.imgmini.eastday.com/mobile/20160912/20160912061828_8c09ffce02b767cdc68d09a680e44625_1_mwpm_03200403.jpeg",
"thumbnail_pic_s02": "http://07.imgmini.eastday.com/mobile/20160912/20160912061828_8c09ffce02b767cdc68d09a680e44625_1_mwpl_05500201.jpeg",
"thumbnail_pic_s03": "http://07.imgmini.eastday.com/mobile/20160912/20160912061828_8c09ffce02b767cdc68d09a680e44625_1_mwpl_05500201.jpeg",
"url": "http://mini.eastday.com/mobile/160912061828549.html?qid=juheshuju",
"uniquekey": "160912061828549",
"type": "头条",
"realtype": "娱乐"
},
{
"title": "赵薇怎么又胖又老,和林心如比起来差这么多?",
"date": "2016-09-12 06:08",
"author_name": "最美Angelababy",
"thumbnail_pic_s": "http://08.imgmini.eastday.com/mobile/20160912/20160912060823_028c9d44155b206f0fc3b5306506fca7_1_mwpm_03200403.jpeg",
"thumbnail_pic_s02": "http://08.imgmini.eastday.com/mobile/20160912/20160912060823_028c9d44155b206f0fc3b5306506fca7_1_mwpl_05500201.jpeg",
"thumbnail_pic_s03": "http://08.imgmini.eastday.com/mobile/20160912/20160912060823_028c9d44155b206f0fc3b5306506fca7_1_mwpl_05500201.jpeg",
"url": "http://mini.eastday.com/mobile/160912060823513.html?qid=juheshuju",
"uniquekey": "160912060823513",
"type": "头条",
"realtype": "娱乐"
},
第二种:
{
"error": 0,
"status": "success",
"date": "2014-05-10",
"results": [
{
"currentCity": "南京",
"weather_data": [
{
"date": "周六(今天, 实时:19℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/dayu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/dayu.png",
"weather": "大雨",
"wind": "东南风5-6级",
"temperature": "18℃"
},
{
"date": "周日",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhenyu.png",
"nightPictureUrl": "http://api.map.baidu.co