美文网首页
Gson 解析中json字符串数组转换为对象list的方法

Gson 解析中json字符串数组转换为对象list的方法

作者: yuezhusust | 来源:发表于2017-10-30 19:06 被阅读0次

    使用Gson将json格式字符串数据转化为对象list的情况中,经常出现json格式字符串参数个数与要转换的类对象不匹配情况,如类对象定义有多个参数,但是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 格式字符串数组:

    [

    {

    "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格式字符串数组

    List<Content> contents = rs.get(0).getContent();

    这样就可以获取到Content类型的对象列表,对于json格式字符串数组中缺少的参数,Gson会自动加上,参数值为参数初始化的默认值。

    相关文章

      网友评论

          本文标题:Gson 解析中json字符串数组转换为对象list的方法

          本文链接:https://www.haomeiwen.com/subject/oarmpxtx.html