1.GSON的两个重要方法
在GSON的API中,提供了两个重要的方法:toJson()和fromJson()方法。其中,toJson()方法用来实现将Java对象转换为相应的JSON数据,fromJson()方法则用来实现将JSON数据转换为相应的Java对象。
1.1 toJson()方法
toJson()方法用于将Java对象转换为相应的JSON数据,主要有以下几种形式:
(1)String toJson(JsonElement jsonElement);
(2)String toJson(Object src);
(3)String toJson(Object src, Type typeOfSrc);
其中,方法(1)用于将JsonElement对象(可以是JsonObject、JsonArray等)转换成JSON数据;方法(2)用于将指定的Object对象序列化成相应的JSON数据;方法(3)用于将指定的Object对象(可以包括泛型类型)序列化成相应的JSON数据。
1.2 fromJson()方法
fromJson()方法用于将JSON数据转换为相应的Java对象,主要有以下几种形式:
(1) T fromJson(JsonElement json, Class classOfT);
(2) T fromJson(JsonElement json, Type typeOfT);
(3) T fromJson(JsonReader reader, Type typeOfT);
(4) T fromJson(Reader reader, Class classOfT);
(5) T fromJson(Reader reader, Type typeOfT);
(6) T fromJson(String json, Class classOfT);
(7) T fromJson(String json, Type typeOfT);
以上的方法用于将不同形式的JSON数据解析成Java对象。
下面我将以一个具体的列子来说明通过Gson方式解析复杂的json数据
1.将要解析的数据如下面的格式
服务的端返回的JSON数据
{
"song_list": [
{
"artist_id": "88",
"language": "国语",
"pic_big": "http://musicdata.baidu.com/data2/pic/76dc8dc35a361ef018c2c52befabfb03/267709259/267709259.jpg",
"pic_small": "http://musicdata.baidu.com/data2/pic/eede55e93e4f0353b1eea0a7627e7be1/267709262/267709262.jpg",
"country": "内地",
"area": "0",
"publishtime": "2015-05-20",
"album_no": "4",
"lrclink": "http://musicdata.baidu.com/data2/lrc/ac66a881bd5cb97ad351936606c37495/266097259/266097259.lrc",
"copy_type": "1",
"hot": "1314161",
"all_artist_ting_uid": "2517",
"resource_type": "0",
"is_new": "0",
"rank_change": "0",
"rank": "1",
"all_artist_id": "88",
"style": "流行",
"del_status": "0",
"relate_status": "0",
"toneid": "0",
"all_rate": "64,128,192,256,320,flac",
"sound_effect": "0",
"file_duration": 0,
"has_mv_mobile": 0,
"versions": "",
"bitrate_fee": "{\"0\":\"0|0\",\"1\":\"0|0\"}",
"song_id": "242078437",
"title": "演员",
"ting_uid": "2517",
"author": "薛之谦",
"album_id": "241838068",
"album_title": "初学者",
"is_first_publish": 0,
"havehigh": 2,
"charge": 0,
"has_mv": 0,
"learn": 1,
"song_source": "web",
"piao_id": "0",
"korean_bb_song": "0",
"resource_type_ext": "0",
"mv_provider": "0000000000",
"artist_name": "薛之谦"
}
],
"billboard": {
"billboard_type": "2",
"billboard_no": "1906",
"update_date": "2016-07-24",
"billboard_songnum": "587",
"havemore": 1,
"name": "热歌榜",
"comment": "该榜单是根据百度音乐平台歌曲每周播放量自动生成的数据榜单,统计范围为百度音乐平台上的全部歌曲,每日更新一次",
"pic_s640": "http://b.hiphotos.baidu.com/ting/pic/item/5d6034a85edf8db1194683910b23dd54574e74df.jpg",
"pic_s444": "http://d.hiphotos.baidu.com/ting/pic/item/c83d70cf3bc79f3d98ca8e36b8a1cd11728b2988.jpg",
"pic_s260": "http://a.hiphotos.baidu.com/ting/pic/item/838ba61ea8d3fd1f1326c83c324e251f95ca5f8c.jpg",
"pic_s210": "http://business.cdn.qianqian.com/qianqian/pic/bos_client_58c1700bf56062108d8d622a95708032.jpg",
"web_url": "http://music.baidu.com/top/dayhot"
},
"error_code": 22000
}
2.必须定义如下一些的实体类数据
注意:在定义实体类是要与服务器返回的json字段要完全一致。
MusicListResponse.java
里边定义的是服务端返回的json数据,遇到[……]我用了list集合定义它的类型
public class MusicListResponse {
private List song_list;
private BillBoard billboard;
private int error_code;
public MusicListResponse() {
}
public MusicListResponse(List song_list, BillBoard billboard,
int error_code) {
super();
this.song_list = song_list;
this.billboard = billboard;
this.error_code = error_code;
}
public List getSong_list() {
return song_list;
}
public void setSong_list(List song_list) {
this.song_list = song_list;
}
public BillBoard getBillboard() {
return billboard;
}
public void setBillboard(BillBoard billboard) {
this.billboard = billboard;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
}
BillBoard.java
这是BillBoard的实体类,里面定义的是 “billboard”: {……}内的json字段
声明它们的无参和有参的构造方法,还有它们的get和set方法
public class BillBoard {
private int havemore;
private String billboard_type;
private String billboard_no;
private String update_date;
private String billboard_songnum;
private String name;
private String comment;
private String pic_s640;
private String pic_s444;
private String pic_s260;
private String pic_s210;
private String web_url;
public BillBoard() {
}
public BillBoard(int havemore, String billboard_type, String billboard_no,
String update_date, String billboard_songnum, String name,
String comment, String pic_s640, String pic_s444, String pic_s260,
String pic_s210, String web_url) {
super();
this.havemore = havemore;
this.billboard_type = billboard_type;
this.billboard_no = billboard_no;
this.update_date = update_date;
this.billboard_songnum = billboard_songnum;
this.name = name;
this.comment = comment;
this.pic_s640 = pic_s640;
this.pic_s444 = pic_s444;
this.pic_s260 = pic_s260;
this.pic_s210 = pic_s210;
this.web_url = web_url;
}
public int getHavemore() {
return havemore;
}
public void setHavemore(int havemore) {
this.havemore = havemore;
}
public String getBillboard_type() {
return billboard_type;
}
public void setBillboard_type(String billboard_type) {
this.billboard_type = billboard_type;
}
public String getBillboard_no() {
return billboard_no;
}
public void setBillboard_no(String billboard_no) {
this.billboard_no = billboard_no;
}
public String getUpdate_date() {
return update_date;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
public String getBillboard_songnum() {
return billboard_songnum;
}
public void setBillboard_songnum(String billboard_songnum) {
this.billboard_songnum = billboard_songnum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getPic_s640() {
return pic_s640;
}
public void setPic_s640(String pic_s640) {
this.pic_s640 = pic_s640;
}
public String getPic_s444() {
return pic_s444;
}
public void setPic_s444(String pic_s444) {
this.pic_s444 = pic_s444;
}
public String getPic_s260() {
return pic_s260;
}
public void setPic_s260(String pic_s260) {
this.pic_s260 = pic_s260;
}
public String getPic_s210() {
return pic_s210;
}
public void setPic_s210(String pic_s210) {
this.pic_s210 = pic_s210;
}
public String getWeb_url() {
return web_url;
}
public void setWeb_url(String web_url) {
this.web_url = web_url;
}
}
Music.java
这是Music的实体类,里面定义的是 “song_list”: [{……},{……}……]内的json字段,声明它们的无参和有参的构造方法,还有它们的get和set方法。(要以服务端返回的数据,完全一致)
public class Music {
private int file_duration;
private int has_mv_mobile;
private int is_first_publish;
private int havehigh;
private int charge;
private int has_mv;
private int learn;
private String artist_id;
private String language;
private String pic_big;
private String pic_small;
private String country;
private String area;
private String publishtime;
private String album_no;
private String lrclink;
private String copy_type;
private String hot;
private String all_artist_ting_uid;
private String resource_type;
private String is_new;
private String rank_change;
private String rank;
private String all_artist_id;
private String style;
private String del_status;
private String relate_status;
private String toneid;
private String all_rate;
private String sound_effect;
private String versions;
private String bitrate_fee;
private String song_id;
private String title;
private String ting_uid;
private String author;
private String album_id;
private String album_title;
private String song_source;
private String piao_id;
private String korean_bb_song;
private String resource_type_ext;
private String mv_provider;
private String artist_name;
public Music() {
}
public Music(int file_duration, int has_mv_mobile, int is_first_publish,
int havehigh, int charge, int has_mv, int learn, String artist_id,
String language, String pic_big, String pic_small, String country,
String area, String publishtime, String album_no, String lrclink,
String copy_type, String hot, String all_artist_ting_uid,
String resource_type, String is_new, String rank_change,
String rank, String all_artist_id, String style, String del_status,
String relate_status, String toneid, String all_rate,
String sound_effect, String versions, String bitrate_fee,
String song_id, String title, String ting_uid, String author,
String album_id, String album_title, String song_source,
String piao_id, String korean_bb_song, String resource_type_ext,
String mv_provider, String artist_name) {
super();
this.file_duration = file_duration;
this.has_mv_mobile = has_mv_mobile;
this.is_first_publish = is_first_publish;
this.havehigh = havehigh;
this.charge = charge;
this.has_mv = has_mv;
this.learn = learn;
this.artist_id = artist_id;
this.language = language;
this.pic_big = pic_big;
this.pic_small = pic_small;
this.country = country;
this.area = area;
this.publishtime = publishtime;
this.album_no = album_no;
this.lrclink = lrclink;
this.copy_type = copy_type;
this.hot = hot;
this.all_artist_ting_uid = all_artist_ting_uid;
this.resource_type = resource_type;
this.is_new = is_new;
this.rank_change = rank_change;
this.rank = rank;
this.all_artist_id = all_artist_id;
this.style = style;
this.del_status = del_status;
this.relate_status = relate_status;
this.toneid = toneid;
this.all_rate = all_rate;
this.sound_effect = sound_effect;
this.versions = versions;
this.bitrate_fee = bitrate_fee;
this.song_id = song_id;
this.title = title;
this.ting_uid = ting_uid;
this.author = author;
this.album_id = album_id;
this.album_title = album_title;
this.song_source = song_source;
this.piao_id = piao_id;
this.korean_bb_song = korean_bb_song;
this.resource_type_ext = resource_type_ext;
this.mv_provider = mv_provider;
this.artist_name = artist_name;
}
public int getFile_duration() {
return file_duration;
}
public void setFile_duration(int file_duration) {
this.file_duration = file_duration;
}
public int getHas_mv_mobile() {
return has_mv_mobile;
}
public void setHas_mv_mobile(int has_mv_mobile) {
this.has_mv_mobile = has_mv_mobile;
}
public int getIs_first_publish() {
return is_first_publish;
}
public void setIs_first_publish(int is_first_publish) {
this.is_first_publish = is_first_publish;
}
public int getHavehigh() {
return havehigh;
}
public void setHavehigh(int havehigh) {
this.havehigh = havehigh;
}
public int getCharge() {
return charge;
}
public void setCharge(int charge) {
this.charge = charge;
}
public int getHas_mv() {
return has_mv;
}
public void setHas_mv(int has_mv) {
this.has_mv = has_mv;
}
public int getLearn() {
return learn;
}
public void setLearn(int learn) {
this.learn = learn;
}
public String getArtist_id() {
return artist_id;
}
public void setArtist_id(String artist_id) {
this.artist_id = artist_id;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getPic_big() {
return pic_big;
}
public void setPic_big(String pic_big) {
this.pic_big = pic_big;
}
public String getPic_small() {
return pic_small;
}
public void setPic_small(String pic_small) {
this.pic_small = pic_small;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getPublishtime() {
return publishtime;
}
public void setPublishtime(String publishtime) {
this.publishtime = publishtime;
}
public String getAlbum_no() {
return album_no;
}
public void setAlbum_no(String album_no) {
this.album_no = album_no;
}
public String getLrclink() {
return lrclink;
}
public void setLrclink(String lrclink) {
this.lrclink = lrclink;
}
public String getCopy_type() {
return copy_type;
}
public void setCopy_type(String copy_type) {
this.copy_type = copy_type;
}
public String getHot() {
return hot;
}
public void setHot(String hot) {
this.hot = hot;
}
public String getAll_artist_ting_uid() {
return all_artist_ting_uid;
}
public void setAll_artist_ting_uid(String all_artist_ting_uid) {
this.all_artist_ting_uid = all_artist_ting_uid;
}
public String getResource_type() {
return resource_type;
}
public void setResource_type(String resource_type) {
this.resource_type = resource_type;
}
public String getIs_new() {
return is_new;
}
public void setIs_new(String is_new) {
this.is_new = is_new;
}
public String getRank_change() {
return rank_change;
}
public void setRank_change(String rank_change) {
this.rank_change = rank_change;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public String getAll_artist_id() {
return all_artist_id;
}
public void setAll_artist_id(String all_artist_id) {
this.all_artist_id = all_artist_id;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getDel_status() {
return del_status;
}
public void setDel_status(String del_status) {
this.del_status = del_status;
}
public String getRelate_status() {
return relate_status;
}
public void setRelate_status(String relate_status) {
this.relate_status = relate_status;
}
public String getToneid() {
return toneid;
}
public void setToneid(String toneid) {
this.toneid = toneid;
}
public String getAll_rate() {
return all_rate;
}
public void setAll_rate(String all_rate) {
this.all_rate = all_rate;
}
public String getSound_effect() {
return sound_effect;
}
public void setSound_effect(String sound_effect) {
this.sound_effect = sound_effect;
}
public String getVersions() {
return versions;
}
public void setVersions(String versions) {
this.versions = versions;
}
public String getBitrate_fee() {
return bitrate_fee;
}
public void setBitrate_fee(String bitrate_fee) {
this.bitrate_fee = bitrate_fee;
}
public String getSong_id() {
return song_id;
}
public void setSong_id(String song_id) {
this.song_id = song_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTing_uid() {
return ting_uid;
}
public void setTing_uid(String ting_uid) {
this.ting_uid = ting_uid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAlbum_id() {
return album_id;
}
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public String getAlbum_title() {
return album_title;
}
public void setAlbum_title(String album_title) {
this.album_title = album_title;
}
public String getSong_source() {
return song_source;
}
public void setSong_source(String song_source) {
this.song_source = song_source;
}
public String getPiao_id() {
return piao_id;
}
public void setPiao_id(String piao_id) {
this.piao_id = piao_id;
}
public String getKorean_bb_song() {
return korean_bb_song;
}
public void setKorean_bb_song(String korean_bb_song) {
this.korean_bb_song = korean_bb_song;
}
public String getResource_type_ext() {
return resource_type_ext;
}
public void setResource_type_ext(String resource_type_ext) {
this.resource_type_ext = resource_type_ext;
}
public String getMv_provider() {
return mv_provider;
}
public void setMv_provider(String mv_provider) {
this.mv_provider = mv_provider;
}
public String getArtist_name() {
return artist_name;
}
public void setArtist_name(String artist_name) {
this.artist_name = artist_name;
}
}
然后具体的实体类定义好了就将解析数据了,下面就是我的解析数据类
public class MainActivity extends Activity {
private RequestQueue queue;
protected MusicListResponse resp;
private private List musics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//发送http请求加载音乐列表数据
loadMusics();
}
/**
* 加载所有音乐数据
*/
private void loadMusics() {
//1.创建volley的 RequestQueue
queue = Volley.newRequestQueue(this);
//2. StringRequest
String url = "http://tingapi.tiantian.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=tiantian.ting.billboard.billList&format=json&type=2&offset=0&size=50";
StringRequest req = new StringRequest(url, new Listener() {
public void onResponse(String response) {
//使用gson解析json字符串
Gson gson = new Gson();
resp = gson.fromJson(response, MusicListResponse.class);
musics = resp.getSong_list();
}
}, new ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.e("error", error.getMessage());
}
});
//3. add()
queue.add(req);
}
}
其中上面的RequestQueue是开源网络库Volley的使用,如果你对该库的使用还不熟悉的话可以参考http://blog.csdn.net/guolin_blog/article/details/17482095,该作者对Volley库的使用讲解得非常的细致和深入
大家可以仔细的去拜读。