【问题标题】:Gson Generics attributeGson 泛型属性
【发布时间】:2013-09-28 09:56:10
【问题描述】:

我对 GSON 库有疑问

这是我的一个类的源代码

public class Response<T>
{
    public int count;
    public ArrayList<T> items;


    public Response()
    {

    }
}


public class AudioDto
{
    public long id;

    public long owner_id;

    public String artist;

    public String title;

    public long duration;

    public String url;

    public long lyrics_id;

    public long album_id;

    public long genre_id;

    @Override
    public String toString()
    {
        return "AudioDto [id=" + id + ", owner_id=" + owner_id + ", artist=" + artist + ", title=" + title + ", duration=" + duration + ", url=" + url + ", lyrics_id=" + lyrics_id + ", album_id=" + album_id + ", genre_id=" + genre_id + "]";
    }



}

这里

        Gson gson = new Gson();

        Response<AudioDto> response = new Response<AudioDto>();
        response = gson.fromJson(responseElement, response.getClass());

问题是:

如何使 GSON 将 JSON 字符串反序列化为 Response 对象,并将所有项目反序列化为 AudioDto 对象。如果我手动指定ArrayList&lt;AudioDto&gt;,它会考虑到“items”字段中的项目是 AudioType 类型的对象,但参数化类型似乎将其转换为 Object 类。

这里是 JSON 字符串

{"count":166,"items":[{"id":231270625,"owner_id":205245503,"artist":"John Newman","title":"Love Me Again","duration":235,"url":"http://cs9-2v4.vk.me/p20/1ee1a056da24cb.mp3","lyrics_id":111547947,"genre_id":17},{"id":230612631,"owner_id":205245503,"artist":"Florence and The Machine","title":"No Light, No Light","duration":274,"url":"http://cs9-5v4.vk.me/p19/51a5b460796306.mp3","lyrics_id":20459437,"genre_id":18},{"id":230612324,"owner_id":205245503,"artist":"Backstreet Boys","title":"Incomplete","duration":239,"url":"http://cs9-4v4.vk.me/p13/b8dcc4cee8bf03.mp3","lyrics_id":268139,"genre_id":1}]}

【问题讨论】:

  • 请发布您要反序列化的Json String (responseElement)
  • 另外请提供您的班级AudioDto
  • 按要求提供。

标签: java json generics gson


【解决方案1】:

首先你不能使用ArrayList&lt;T&gt; items;,因为Gson试图将它转换成LinkedList

所以请改用List&lt;T&gt;

之后,你可以试试:

GsonBuilder gsonB = new GsonBuilder();
Type collectionType = new TypeToken<Response<AudioDto>>() {}.getType();
//Response<AudioDto> response = new Response<AudioDto>();  // you don't need this row

Response<AudioDto> response = gsonB.create().fromJson(responseElement, collectionType);

//assert(response != null);

顺便说一句,使用Gson gson = new Gson(); 代替GsonBuilder gsonB = new GsonBuilder();

那里没有什么可配置的。

关于类型

据我所知,您无法创建 Type&lt;T&gt;。但是您可以改用&lt;AudioDto&gt; 中的Type

启动器类

....
LoadJson<AudioDto> lj = new LoadJson<AudioDto>();
Type collectionType = new TypeToken<Response<AudioDto>>() {}.getType();        
Response<AudioDto> response  = lj.load(responseElement, collectionType);  

LoadJson 类

 public class LoadJson<T> {

 Response<T> load(String responseElement, Type classType){

  Gson gson = new Gson();

    Response<T> response = gson.fromJson(responseElement, classType);

  return response;
  }
}

【讨论】:

  • 这就是我需要的!我一直在尝试这样的事情,尽管我使用的是 ArrayList 而不是 List。谢谢!!!
  • 不幸的是,没有办法这样做:public class Response&lt;T&gt; { public int count; public List&lt;T&gt; items; public Type classType = new TypeToken&lt;Response&lt;T&gt;&gt;() {}.getType(); }
猜你喜欢
  • 1970-01-01
  • 2011-10-08
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多