Gson是google官方推出的解析json数据的jar包,其操作简单,解析速度快。
Gson jar包下载地址:http://code.google.com/p/google-gson/
接下来就说一下使用Gson解析json的具体步骤:
a:根据json数据的内容创建实体类( 注:实体类的属性名称必须与json中的数据的字段名一致,否则会出错)
b:创建Gson对象,通过调用Gson对象.fromJson(json,实体类.class)方法实现解析;
实例代码:
json数据地址:http://open.qyer.com/qyer/footprint/city_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=865174021302911&track_app_version=6.8.1&track_app_channel=360m&track_device_info=HWHol-T&track_os=Android4.2.2&app_installtime=1446561269801&lat=40.113794&lon=116.24896&city_id=8399
实体类:
根据json数据创建实体类:
public class News {
/**
* status : 1
* info :
* times : 0
* data : {"id":8399,"country_id":517,"planto":0,"beento":0,"overview_url":"http://appview.qyer.com/place/cape-town/profile","selecthotel_url":"http://appview.qyer.com/index.php?action=hotelDetail&spm=index&aid=352438&cityid=8399","photos":["http://pic.qyer.com/album/user/344/33/Qk1RQRkCYQ/index/w800","http://pic.qyer.com/album/1d6/c4/889014/index/w800"],"has_guide":true,"has_plan":true,"country_cnname":"南非","country_enname":"South Africa","cnname":"开普敦","enname":"Cape Town","has_trip":true,"entryCont":"开普敦(Cape Town)是南非人口排名第二大城市,也是开普敦都会城区的组成部分、西开普省省会,开普敦为南非立法首都,因此南非国会及很多*部门亦座落于该市。开普敦以其美丽的自然景观及码头闻名,知名的地标有被誉为\u201c上帝之餐桌\u201d的桌山,以及印度洋和大西洋的交汇点好望角。因其美丽的自然及地理环境,开普","hot_mguide":[{"id":561,"photo":"http://pic.qyer.com/album/1d5/f9/1862040/index/710x360","title":"开普敦淘宝的好去处","user_id":3673979,"username":"周阿紫","avatar":"http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272"},{"id":559,"photo":"http://pic.qyer.com/album/user/343/57/Qk1WRx0EYw/index/710x360","title":"开普敦的最佳摄影地","user_id":3673979,"username":"周阿紫","avatar":"http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272"},{"id":560,"photo":"http://pic.qyer.com/album/user/344/33/Qk1RQRkDaQ/index/710x360","title":"开普敦周边的特色小镇","user_id":3673979,"username":"周阿紫","avatar":"http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272"},{"id":2639,"photo":"http://pic.qyer.com/album/user/987/34/SEFSQR4CaA/index/710x360","title":"南非花园大道上吃到high,也是一种醉!","user_id":2716783,"username":"罌樹","avatar":"http://static.qyer.com/data/avatar/002/71/67/83_avatar_big.jpg?v=1394785986"}],"new_discount":[{"id":50276,"title":"达人支招出国必带手信,要买对哟~","price":"<em>115<\/em>元起","priceoff":"","expire_date":"2015年12月31日结束","photo":"http://pic.qyer.com/public/lastmin/lastminute/2015/08/04/14386823652828/300x200"}],"local_discount":[]}
* ra_referer :
*/
private int status;
private String info;
private int times;
/**
* id : 8399
* country_id : 517
* planto : 0
* beento : 0
* overview_url : http://appview.qyer.com/place/cape-town/profile
* selecthotel_url : http://appview.qyer.com/index.php?action=hotelDetail&spm=index&aid=352438&cityid=8399
* photos : ["http://pic.qyer.com/album/user/344/33/Qk1RQRkCYQ/index/w800","http://pic.qyer.com/album/1d6/c4/889014/index/w800"]
* has_guide : true
* has_plan : true
* country_cnname : 南非
* country_enname : South Africa
* cnname : 开普敦
* enname : Cape Town
* has_trip : true
* entryCont : 开普敦(Cape Town)是南非人口排名第二大城市,也是开普敦都会城区的组成部分、西开普省省会,开普敦为南非立法首都,因此南非国会及很多*部门亦座落于该市。开普敦以其美丽的自然景观及码头闻名,知名的地标有被誉为“上帝之餐桌”的桌山,以及印度洋和大西洋的交汇点好望角。因其美丽的自然及地理环境,开普
* hot_mguide : [{"id":561,"photo":"http://pic.qyer.com/album/1d5/f9/1862040/index/710x360","title":"开普敦淘宝的好去处","user_id":3673979,"username":"周阿紫","avatar":"http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272"},{"id":559,"photo":"http://pic.qyer.com/album/user/343/57/Qk1WRx0EYw/index/710x360","title":"开普敦的最佳摄影地","user_id":3673979,"username":"周阿紫","avatar":"http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272"},{"id":560,"photo":"http://pic.qyer.com/album/user/344/33/Qk1RQRkDaQ/index/710x360","title":"开普敦周边的特色小镇","user_id":3673979,"username":"周阿紫","avatar":"http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272"},{"id":2639,"photo":"http://pic.qyer.com/album/user/987/34/SEFSQR4CaA/index/710x360","title":"南非花园大道上吃到high,也是一种醉!","user_id":2716783,"username":"罌樹","avatar":"http://static.qyer.com/data/avatar/002/71/67/83_avatar_big.jpg?v=1394785986"}]
* new_discount : [{"id":50276,"title":"达人支招出国必带手信,要买对哟~","price":"<em>115<\/em>元起","priceoff":"","expire_date":"2015年12月31日结束","photo":"http://pic.qyer.com/public/lastmin/lastminute/2015/08/04/14386823652828/300x200"}]
* local_discount : []
*/
private DataEntity data;
private String ra_referer;
public void setStatus(int status) {
this.status = status;
}
public void setInfo(String info) {
this.info = info;
}
public void setTimes(int times) {
this.times = times;
}
public void setData(DataEntity data) {
this.data = data;
}
public void setRa_referer(String ra_referer) {
this.ra_referer = ra_referer;
}
public int getStatus() {
return status;
}
public String getInfo() {
return info;
}
public int getTimes() {
return times;
}
public DataEntity getData() {
return data;
}
public String getRa_referer() {
return ra_referer;
}
public static class DataEntity {
private int id;
private int country_id;
private int planto;
private int beento;
private String overview_url;
private String selecthotel_url;
private boolean has_guide;
private boolean has_plan;
private String country_cnname;
private String country_enname;
private String cnname;
private String enname;
private boolean has_trip;
private String entryCont;
private List<String> photos;
/**
* id : 561
* photo : http://pic.qyer.com/album/1d5/f9/1862040/index/710x360
* title : 开普敦淘宝的好去处
* user_id : 3673979
* username : 周阿紫
* avatar : http://static.qyer.com/data/avatar/003/67/39/79_avatar_big.jpg?v=1404456272
*/
private List<HotMguideEntity> hot_mguide;
/**
* id : 50276
* title : 达人支招出国必带手信,要买对哟~
* price : <em>115</em>元起
* priceoff :
* expire_date : 2015年12月31日结束
* photo : http://pic.qyer.com/public/lastmin/lastminute/2015/08/04/14386823652828/300x200
*/
private List<NewDiscountEntity> new_discount;
private List<?> local_discount;
public void setId(int id) {
this.id = id;
}
public void setCountry_id(int country_id) {
this.country_id = country_id;
}
public void setPlanto(int planto) {
this.planto = planto;
}
public void setBeento(int beento) {
this.beento = beento;
}
public void setOverview_url(String overview_url) {
this.overview_url = overview_url;
}
public void setSelecthotel_url(String selecthotel_url) {
this.selecthotel_url = selecthotel_url;
}
public void setHas_guide(boolean has_guide) {
this.has_guide = has_guide;
}
public void setHas_plan(boolean has_plan) {
this.has_plan = has_plan;
}
public void setCountry_cnname(String country_cnname) {
this.country_cnname = country_cnname;
}
public void setCountry_enname(String country_enname) {
this.country_enname = country_enname;
}
public void setCnname(String cnname) {
this.cnname = cnname;
}
public void setEnname(String enname) {
this.enname = enname;
}
public void setHas_trip(boolean has_trip) {
this.has_trip = has_trip;
}
public void setEntryCont(String entryCont) {
this.entryCont = entryCont;
}
public void setPhotos(List<String> photos) {
this.photos = photos;
}
public void setHot_mguide(List<HotMguideEntity> hot_mguide) {
this.hot_mguide = hot_mguide;
}
public void setNew_discount(List<NewDiscountEntity> new_discount) {
this.new_discount = new_discount;
}
public void setLocal_discount(List<?> local_discount) {
this.local_discount = local_discount;
}
public int getId() {
return id;
}
public int getCountry_id() {
return country_id;
}
public int getPlanto() {
return planto;
}
public int getBeento() {
return beento;
}
public String getOverview_url() {
return overview_url;
}
public String getSelecthotel_url() {
return selecthotel_url;
}
public boolean isHas_guide() {
return has_guide;
}
public boolean isHas_plan() {
return has_plan;
}
public String getCountry_cnname() {
return country_cnname;
}
public String getCountry_enname() {
return country_enname;
}
public String getCnname() {
return cnname;
}
public String getEnname() {
return enname;
}
public boolean isHas_trip() {
return has_trip;
}
public String getEntryCont() {
return entryCont;
}
public List<String> getPhotos() {
return photos;
}
public List<HotMguideEntity> getHot_mguide() {
return hot_mguide;
}
public List<NewDiscountEntity> getNew_discount() {
return new_discount;
}
public List<?> getLocal_discount() {
return local_discount;
}
public static class HotMguideEntity {
private int id;
private String photo;
private String title;
private int user_id;
private String username;
private String avatar;
public void setId(int id) {
this.id = id;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public void setTitle(String title) {
this.title = title;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public void setUsername(String username) {
this.username = username;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public int getId() {
return id;
}
public String getPhoto() {
return photo;
}
public String getTitle() {
return title;
}
public int getUser_id() {
return user_id;
}
public String getUsername() {
return username;
}
public String getAvatar() {
return avatar;
}
}
public static class NewDiscountEntity {
private int id;
private String title;
private String price;
private String priceoff;
private String expire_date;
private String photo;
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrice(String price) {
this.price = price;
}
public void setPriceoff(String priceoff) {
this.priceoff = priceoff;
}
public void setExpire_date(String expire_date) {
this.expire_date = expire_date;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getPrice() {
return price;
}
public String getPriceoff() {
return priceoff;
}
public String getExpire_date() {
return expire_date;
}
public String getPhoto() {
return photo;
}
}
}
}
解析数据:
只需一行代码:News news=new Gson().fromJson(json,News.class);
通过上面这一句话就将json中的数据全部封装在News这个实体类中了,接下来该怎么操作就看自己的实际情况了。