使用Gson將json格式字符串數(shù)據(jù)轉化為對象list的情況中道媚,經(jīng)常出現(xiàn)json格式字符串參數(shù)個數(shù)與要轉換的類對象不匹配情況跺讯,如類對象定義有多個參數(shù)碟绑,但是json格式字符串只有其中的一部分烤惊,這時候直接調用Gson 提供的?T fromJson(String json, Type typeOfT)方法轉換會報錯溉奕,會提示json格式轉換不匹配錯誤压语。
? ? ?下面例子的方法可以解決這個問題啸罢。
1. 對象類定義:
public class Content implements Serializable {
private int formType;
private String name;
private String country;
private int intergrity;
private int broadcastType;
private String broadcastAge;
private String issuer;
private String language;
private String url;
private int currentTime;
private String contentId;
private String pContentId;
private int next;
public Content() {
}
public int getFormType() {
return this.formType;
}
public void setFormType(int formType) {
this.formType = formType;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
public int getIntergrity() {
return this.intergrity;
}
public void setIntergrity(int intergrity) {
this.intergrity = intergrity;
}
public int getBroadcastType() {
return this.broadcastType;
}
public void setBroadcastType(int broadcastType) {
this.broadcastType = broadcastType;
}
public String getBroadcastAge() {
return this.broadcastAge;
}
public void setBroadcastAge(String broadcastAge) {
this.broadcastAge = broadcastAge;
}
public String getIssuer() {
return this.issuer;
}
public void setIssuer(String issuer) {
this.issuer = issuer;
}
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public int getCurrentTime() {
return this.currentTime;
}
public void setCurrentTime(int currentTime) {
this.currentTime = currentTime;
}
public String getContentId() {
return this.contentId;
}
public void setContentId(String contentId) {
this.contentId = contentId;
}
public String getPContentId() {
return this.pContentId;
}
public void setPContentId(String pContentId) {
this.pContentId = pContentId;
}
public int getNext() {
return this.next;
}
public void setNext(int next) {
this.next = next;
}
public String toString() {
return "Content{formType=" + this.formType + ", name='" + this.name + '\'' + ", country='" + this.country + '\'' + ", intergrity=" + this.intergrity + ", broadcastType=" + this.broadcastType + ", broadcastAge='" + this.broadcastAge + '\'' + ", issuer='" + this.issuer + '\'' + ", language='" + this.language + '\'' + ", url='" + this.url + '\'' + ", currentTime=" + this.currentTime + ", contentId='" + this.contentId + '\'' + ", pContentId='" + this.pContentId + '\'' + ", next=" + this.next + '}';
}
}
2. json 格式字符串數(shù)組:
[
{
"Content": [
{
"url": "",
"currentTime": 0,
"contentId": 631584528
}
]
}
]
3. 轉換方式為
(1) 新建一個java類,類名稱叫DataBean,類代碼如下:
public class DataBean {
List <Content >?Content;
public List getContent() {
returnContent;
}
public void setContent(List content) {
this.Content= content;
}
}
(2) 調用Gson解析
Gson gson =new Gson();
Type type =newTypeToken<ArrayList<DataBean>>() {
}.getType();
List rs = gson.fromJson(data, type);? //data就是2里面的json格式字符串數(shù)組
List<Content>?contents = rs.get(0).getContent();
這樣就可以獲取到Content類型的對象列表胎食,對于json格式字符串數(shù)組中缺少的參數(shù)扰才,Gson會自動加上,參數(shù)值為參數(shù)初始化的默認值厕怜。