Android中网络请求返回json的两种解析方式
当我们请求网络获得json数据后可以通过两种方式去解析json:
1.利用goolge提供给的Gson类:
第一步:引用Google的jar包gson.jar.
第二步:创建gson类,new gson(),并调用gson里的fromJson()方法:
Gson gson = new Gson();
//result:请求网络返回的json,Newstetail.class:解析json需要的泛型类文件
Newstetail mNewstetailData = gson.fromJson(result, Newstetail.class);
例如json:
{
"data": {
"countcommenturl": "http://zhbj.qianlong.com/client/content/countComment/",
"more": "/10007/list_2.json",
"news": [
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35311,
"listimage": "http://10.0.2.2:8080/zhbj/10007/2078369924F9UO.jpg",
"pubdate": "2014-10-1113:18",
"title": "网上大讲堂第368期预告:义务环保人人有责",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35312,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1509585620ASS3.jpg",
"pubdate": "2014-10-1111:20",
"title": "马路改建为停车场车位年费高达3000元",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35313,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1506815057D99I.jpg",
"pubdate": "2014-10-1110:34",
"title": "北京两年内将迁出1200家工业污染企业",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35314,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1505891536Z82T.jpg",
"pubdate": "2014-10-1110:08",
"title": "大雾再锁京城机场航班全部延误",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35315,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1483727032VMXT.jpg",
"pubdate": "2014-10-1110:03",
"title": "APEC会议期间调休企业员工盼同步放假",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35316,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1481879990BEMG.jpg",
"pubdate": "2014-10-1109:59",
"title": "机械“龙马”巡演17日亮相奥园",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35310,
"listimage": "http://10.0.2.2:8080/zhbj/10007/14800329488K7F.jpg",
"pubdate": "2014-10-1109:54",
"title": "门头沟获批100套限价房",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35319",
"id": 35318,
"listimage": "http://10.0.2.2:8080/zhbj/10007/14791094274LT9.jpg",
"pubdate": "2014-10-1109:52",
"title": "APEC期间净空区放带灯风筝可拘留",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35314",
"id": 35314,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1478185906G9WQ.jpg",
"pubdate": "2014-10-1109:48",
"title": "今起两自住房摇号",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35117",
"id": 35117,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1477262385PASS.jpg",
"pubdate": "2014-10-1109:45",
"title": "故宫神武门广场拟夜间开放停车",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
}
],
"title": "北京",
"topic": [
{
"description": "11111111",
"id": 10101,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1452327318UU91.jpg",
"sort": 1,
"title": "北京",
"url": "http://10.0.2.2:8080/zhbj/10007/list_1.json"
},
{
"description": "22222222",
"id": 10100,
"listimage": "http://10.0.2.2:8080/zhbj/10007/1452327318UU91.jpg",
"sort": 2,
"title": "222222222222",
"url": "http://10.0.2.2:8080/zhbj/10007/list_1.json"
}
],
"topnews": [
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35301",
"id": 35301,
"pubdate": "2014-04-0814:24",
"title": "蜗居生活",
"topimage": "http://10.0.2.2:8080/zhbj/10007/1452327318UU91.jpg",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35125",
"id": 35125,
"pubdate": "2014-04-0809:09",
"title": "中秋赏月",
"topimage": "http://10.0.2.2:8080/zhbj/10007/1452327318UU92.jpg",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35125",
"id": 35126,
"pubdate": "2014-04-0809:09",
"title": "天空翱翔",
"topimage": "http://10.0.2.2:8080/zhbj/10007/1452327318UU93.jpg",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
},
{
"comment": true,
"commentlist": "http://10.0.2.2:8080/zhbj/10007/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/35125",
"id": 35127,
"pubdate": "2014-04-0809:09",
"title": "感官设计",
"topimage": "http://10.0.2.2:8080/zhbj/10007/1452327318UU94.png",
"type": "news",
"url": "http://10.0.2.2:8080/zhbj/10007/724D6A55496A11726628.html"
}
]
},
"retcode": 200
}
要创建对应的泛型类Newstetail,遇到{},就创建对象,遇到[]创建集合:
public class Newstetail {
public NewsDate data;
public class NewsDate{
public ArrayList<tetailNews>news;
public String more;
public ArrayList<tetailTopNews>topnews;
}
public class tetailNews{
public int id;
public String listimage;
public String title;
public String type;
public String url;
public String pubdate;
}
public class tetailTopNews{
public int id;
public String listimage;
public String title;
public String type;
public String url;
public String topimage;
}
}
解析后json数据就会被封装在泛型类中.
2.JSONObject
@Override
public ArrayList<AppInfo> parseData(String result) {
try {
JSONObject jsonObject = new JSONObject(result);//遇到{}就new一个JSONObject
JSONArray ja = jsonObject.getJSONArray("list");//遇到[]就new一个Array
//遍历这个ja得到每一个元素
for (int i = 0; i < ja.length(); i++) {
AppInfo appInfo = new AppInfo();
JSONObject jo1 = ja.getJSONObject(i);
appInfo.des=jo1.getString("des");
appInfo.downloadUrl=jo1.getString("downloadUrl");
appInfo.iconUrl=jo1.getString("iconUrl");
appInfo.id=jo1.getString("id");
appInfo.name=jo1.getString("name");
appInfo.packageName=jo1.getString("packageName");
appInfo.size=jo1.getLong("size");
appInfo.stars=(float) jo1.getDouble("stars");
homeList.add(appInfo);
}
ArrayList<String> pictureList = new ArrayList<String>();
JSONArray ja1 = jsonObject.getJSONArray("picture");
for (int i = 0; i < ja1.length(); i++) {
pictureList.add(ja1.getString(i));
}
return homeList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
AppInfo:
public class AppInfo {
/*
"des": "产品介绍:有缘是时下最受大众单身男女亲睐的婚恋交友软件。有缘网专注于通过轻松、",
"downloadUrl": "app/com.youyuan.yyhl/com.youyuan.yyhl.apk",
"iconUrl": "app/com.youyuan.yyhl/icon.jpg",
"id": 1525490,
"name": "有缘网",
"packageName": "com.youyuan.yyhl",
"size": 3876203,
"stars": 4
*/
public String des;
public String downloadUrl;
public String iconUrl;
public String id;
public String name;
public String packageName;
public long size;
public float stars;
}
json:
{
"list": [
{
"des": "产品介绍:有缘是时下最受大众单身男女亲睐的婚恋交友软件。有缘网专注于通过轻松、",
"downloadUrl": "app/com.youyuan.yyhl/com.youyuan.yyhl.apk",
"iconUrl": "app/com.youyuan.yyhl/icon.jpg",
"id": 1525490,
"name": "有缘网",
"packageName": "com.youyuan.yyhl",
"size": 3876203,
"stars": 4
},
{
"des": "全新改版震撼发布!让您赏心悦目、随心所欲、乐在其中、知音识趣......还有多项",
"downloadUrl": "app/com.kugou.android/com.kugou.android.apk",
"iconUrl": "app/com.kugou.android/icon.jpg",
"id": 1599700,
"name": "酷狗音乐",
"packageName": "com.kugou.android",
"size": 8790060,
"stars": 4
},
{
"des": "【网购神器】唯品会,一家专门做特卖的网站,专业买手把脉潮流热点,轻松打造女神范",
"downloadUrl": "app/com.achievo.vipshop/com.achievo.vipshop.apk",
"iconUrl": "app/com.achievo.vipshop/icon.jpg",
"id": 1633797,
"name": "唯品会",
"packageName": "com.achievo.vipshop",
"size": 5142608,
"stars": 4
},
{
"des": "男性私人医生是一款专门针对男性生理健康以及其他相关专科病症提供免费在线咨询的医疗",
"downloadUrl": "app/com.medapp.man/com.medapp.man.apk",
"iconUrl": "app/com.medapp.man/icon.jpg",
"id": 1631363,
"name": "男性私人医生",
"packageName": "com.medapp.man",
"size": 1979701,
"stars": 4
},
{
"des": "就算生活再苦逼,我们仍需小快乐!笑霸来了,给你不一样的快乐。独乐乐,不如众乐乐。",
"downloadUrl": "app/com.zuobao.xiaobao/com.zuobao.xiaobao.apk",
"iconUrl": "app/com.zuobao.xiaobao/icon.jpg",
"id": 1637000,
"name": "笑霸来了(世界杯版)",
"packageName": "com.zuobao.xiaobao",
"size": 4365674,
"stars": 4.5
},
{
"des": "欧朋浏览器7.8.8——最快的手机浏览器,没有之一,欢迎挑战。纤小的身体蕴藏着",
"downloadUrl": "app/com.oupeng.mini.android/com.oupeng.mini.android.apk",
"iconUrl": "app/com.oupeng.mini.android/icon.jpg",
"id": 1521945,
"name": "欧朋浏览器",
"packageName": "com.oupeng.mini.android",
"size": 2376917,
"stars": 3.5
},
{
"des": "蘑菇街是国内最受年轻女性喜爱的购物平台,每天由专业时尚编辑为你搜罗最新最时尚的热",
"downloadUrl": "app/com.mogujie/com.mogujie.apk",
"iconUrl": "app/com.mogujie/icon.jpg",
"id": 1521914,
"name": "蘑菇街",
"packageName": "com.mogujie",
"size": 6746678,
"stars": 3.5
},
{
"des": "★用户量过亿的新闻阅读客户端★占领AppStore新闻类榜首15个月★每天用",
"downloadUrl": "app/com.ss.android.article.news/com.ss.android.article.news.apk",
"iconUrl": "app/com.ss.android.article.news/icon.jpg",
"id": 1631309,
"name": "今日头条",
"packageName": "com.ss.android.article.news",
"size": 5599469,
"stars": 4.5
},
{
"des": "书虫必备小说阅读神器,功能体验全面升级,全网小说任你搜。轻松离线,让你随时随地自",
"downloadUrl": "app/com.shuqi.controller/com.shuqi.controller.apk",
"iconUrl": "app/com.shuqi.controller/icon.jpg",
"id": 1615630,
"name": "书旗小说",
"packageName": "com.shuqi.controller",
"size": 5383460,
"stars": 3.5
},
{
"des": "美团团购,口碑最好、国内最火的团购必备软件,为您提供各城市美食、酒店、娱乐、电影",
"downloadUrl": "app/com.sankuai.meituan/com.sankuai.meituan.apk",
"iconUrl": "app/com.sankuai.meituan/icon.jpg",
"id": 1621882,
"name": " 美团 ",
"packageName": "com.sankuai.meituan",
"size": 6243161,
"stars": 4.5
},
{
"des": "金山电池医生,可延长手机1倍待机时间.专业充电,智能省电,精准预测可用时间,超过",
"downloadUrl": "app/com.ijinshan.kbatterydoctor/com.ijinshan.kbatterydoctor.apk",
"iconUrl": "app/com.ijinshan.kbatterydoctor/icon.jpg",
"id": 1631260,
"name": "金山电池医生",
"packageName": "com.ijinshan.kbatterydoctor",
"size": 4652996,
"stars": 4.5
},
{
"des": "百万高清壁纸、高品质动态壁纸、海量炫酷铃声,最in美图、最强适配、最神吐槽,尽在",
"downloadUrl": "app/com.androidesk/com.androidesk.apk",
"iconUrl": "app/com.androidesk/icon.jpg",
"id": 1640629,
"name": "安卓壁纸",
"packageName": "com.androidesk",
"size": 2989473,
"stars": 4
},
{
"des": "全球150多个国家超过1亿人使用的社交产品。陌陌是国内领先的移动社交产品。你可",
"downloadUrl": "app/com.immomo.momo/com.immomo.momo.apk",
"iconUrl": "app/com.immomo.momo/icon.jpg",
"id": 1565716,
"name": "陌陌",
"packageName": "com.immomo.momo",
"size": 11388135,
"stars": 2.5
},
{
"des": "【激情世界杯,天天好音质】世界杯期间在天天动听#用户评价#里5星好评并带上个人",
"downloadUrl": "app/com.sds.android.ttpod/com.sds.android.ttpod.apk",
"iconUrl": "app/com.sds.android.ttpod/icon.jpg",
"id": 1644521,
"name": "天天动听",
"packageName": "com.sds.android.ttpod",
"size": 9514161,
"stars": 3.5
},
{
"des": "安智市场是目前中国最知名的Android系统手机应用软件免费下载平台。其人性化的",
"downloadUrl": "app/cn.goapk.market/cn.goapk.market.apk",
"iconUrl": "app/cn.goapk.market/icon.jpg",
"id": 1523395,
"name": "安智市场(通用版)",
"packageName": "cn.goapk.market",
"size": 2403320,
"stars": 4
},
{
"des": "亚马逊为Android系统设计的亚马逊手机客户端不仅可以让用户通过一个渐变",
"downloadUrl": "app/cn.amazon.mShop.android/cn.amazon.mShop.android.apk",
"iconUrl": "app/cn.amazon.mShop.android/icon.jpg",
"id": 1608378,
"name": "亚马逊购物",
"packageName": "cn.amazon.mShop.android",
"size": 7351495,
"stars": 3.5
},
{
"des": "1、视频聚合——暴风拥有丰富的720P高清视频,视频内容涵盖了电影、电视剧、动漫",
"downloadUrl": "app/com.storm.smart/com.storm.smart.apk",
"iconUrl": "app/com.storm.smart/icon.jpg",
"id": 1624704,
"name": "暴风影音",
"packageName": "com.storm.smart",
"size": 10205276,
"stars": 2.5
},
{
"des": "一分钱领五元红包!即日起至6月30日,支付宝未绑定银行卡且未快捷支付过的用户登录",
"downloadUrl": "app/com.eg.android.AlipayGphone/com.eg.android.AlipayGphone.apk",
"iconUrl": "app/com.eg.android.AlipayGphone/icon.jpg",
"id": 1540513,
"name": "支付宝钱包",
"packageName": "com.eg.android.AlipayGphone",
"size": 19137654,
"stars": 3
},
{
"des": "ES,全球主动下载超过2亿次,安卓文件管理第一品牌。想更好地管理你的手机、平板",
"downloadUrl": "app/com.estrongs.android.pop/com.estrongs.android.pop.apk",
"iconUrl": "app/com.estrongs.android.pop/icon.jpg",
"id": 1643701,
"name": "ES文件浏览器",
"packageName": "com.estrongs.android.pop",
"size": 4391508,
"stars": 3.5
},
{
"des": "百度贴吧手机客户端---为兴趣而生!百度贴吧是以兴趣主题聚合志同道合者的互",
"downloadUrl": "app/com.baidu.tieba/com.baidu.tieba.apk",
"iconUrl": "app/com.baidu.tieba/icon.jpg",
"id": 1604213,
"name": "百度贴吧",
"packageName": "com.baidu.tieba",
"size": 24574809,
"stars": 3
}
],
"picture": [
"image/home01.jpg",
"image/home02.jpg",
"image/home03.jpg",
"image/home04.jpg",
"image/home05.jpg",
"image/home06.jpg",
"image/home07.jpg",
"image/home08.jpg"
]
}