我对 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<AudioDto>
它会考虑到“项目”字段中的项目是 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}]}