Java Gson泛型属性

Java Gson泛型属性,java,json,generics,gson,Java,Json,Generics,Gson,我有一个关于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 Strin

我有一个关于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 + "]";
    }



}

首先,您不能使用
ArrayList项LinkedList

因此,请使用
列表

之后,您可以尝试:

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);
LoadJson类

公共类LoadJson{
响应加载(字符串响应元素,类型classType){
Gson Gson=新的Gson();
Response-Response=gson.fromJson(responseElement,classType);
返回响应;
}
}

请发布您想要反序列化的Json字符串(responseElement),因此请按照要求提供您的类
AudioDto
。这就是我需要的!我一直在尝试类似的东西,不过,我使用的是ArrayList而不是List。非常感谢。不幸的是,没有办法这样做:
public类响应{public int count;public List items;public Type classType=new-TypeToken(){}.getType();}
{"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}]}
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);
....
LoadJson<AudioDto> lj = new LoadJson<AudioDto>();
Type collectionType = new TypeToken<Response<AudioDto>>() {}.getType();        
Response<AudioDto> response  = lj.load(responseElement, collectionType);  
 public class LoadJson<T> {

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

  Gson gson = new Gson();

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

  return response;
  }
}