简体   繁体   English

使用gson反序列化嵌套的泛型类

[英]De-serializing nested, generic class with gson

Using Gson, I'm trying to de-serialize aa nested, generic class. 使用Gson,我试图反序列化一个嵌套的泛型类。 The class structure looks like the following: 类结构如下所示:

Wrapper object, simplified, but normally holds other properties such as statusMessage , which are returned along with the data -field from the server: 包装器对象,简化,但通常包含其他属性,如statusMessage ,它们与服务器的data字段一起返回:

public class Response<T> {

   private List<T> data = null;

   public List<T> getData() { return this.data; }   

}

Simple class, the expected output from data -field above (though as an array): 简单类,上面data字段的预期输出(尽管作为数组):

public class Language {
   public String alias;
   public String label;
}

Usage: 用法:

Type type = new TypeToken<Response<Language>>() {}.getType();
Response<Language> response = new Gson().fromJson(json, type);
List<Language> languages = response.getData();
Language l = languages.get(0);
System.out.println(l.alias); // Error occurs here

Where the json -variable is something like this . json -variable就是这样的

However, when doing this, I recieve the following exception (on line 3, last code example): 但是,在执行此操作时,我收到以下异常(在第3行,最后一个代码示例):

ClassCastException: com.google.gson.internal.StringMap cannot be cast to book.Language ClassCastException:com.google.gson.internal.StringMap无法强制转换为book.Language

The exception ONLY occurs when storing the data from getData() into a variable ( or when used as one ). 仅将getData()的数据存储到变量中( 或作为一个变量使用时 getData()才会发生异常。

Any help would be highly appreciated. 任何帮助将受到高度赞赏。

The problem you're actually having is not directly due to Gson , it's because of how arrays and Generics play together. 你实际遇到的问题并非直接归因于Gson ,而是因为数组和泛型如何一起发挥作用。

You'll find that you can't actually do new T[10] in a class like yours. 你会发现你实际上不能在像你这样的课堂上做new T[10] see: How to create a generic array in Java? 请参阅: 如何在Java中创建通用数组?

You basically have two options: 你基本上有两个选择:

  1. Write a custom deserializer and construct the T[] array there as shown in the SO question I linked above 编写一个自定义反序列化器并在那里构造T[]数组,如上面链接的SO问题所示
  2. Use a List<T> instead, then it will simply work. 使用List<T>代替,然后它将只是工作。 If you really need to return an array, you can always just call List.toArray() in your method. 如果你真的需要返回一个数组,你总是可以在你的方法中调用List.toArray()

Edited from comments below: 编辑来自以下评论:

This is a fully working example: 这是一个完全有效的例子:

public class App 
{
    public static void main( String[] args )
    {
        String json = "{\"data\": [{\"alias\": \"be\",\"label\": \"vitryska\"},{\"alias\": \"vi\",\"label\": \"vietnamesiska\"},{\"alias\": \"hu\",\"label\": \"ungerska\"},{\"alias\": \"uk\",\"label\": \"ukrainska\"}]}";
        Type type = new TypeToken<Response<Language>>(){}.getType();
        Response<Language> resp = new Gson().fromJson(json, type);

        Language l = resp.getData().get(0);
        System.out.println(l.alias);

    }
}

class Response<T> {

   private List<T> data = null;

   public List<T> getData() { return this.data; }   

}

class Language {
   public String alias;
   public String label;
}

Output: 输出:

be

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM