java - 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 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>它考虑到“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}]}

最佳答案

首先你不能使用ArrayList<T> items;因为 Gson 尝试将其转换为 LinkedList .

所以使用List<T>相反。

之后,您可以尝试:

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<T> 。但你可以使用Type<AudioDto>相反:

启动器类

....
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;
  }
}

关于java - Gson 泛型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065638/

相关文章:

Java 8 升级后,Java applet 无法在 IE 中加载

xml - 在 REST 服务中使用 json 和 xml

c# - 解决 C# 中泛型中缺少 'new(parameters)' 的问题

javascript - 将 XML 数据转换为 Json 格式 AngularJS

java - 如何通过反射调用方法,并提供方法作为参数

swift - Swift 中的泛型运算符重载

java - Spring-data-jpa + Hibernate 没有创建预期的表

java - 在 .jar 文件中导出 java 库

java - Intent 中的 Action_Send_Multiple 不起作用

mysql - Sequelize JSON 数据类型