【问题标题】:java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.classjava.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 无法转换为 pojo.class
【发布时间】:2020-09-20 16:01:03
【问题描述】:

获取 java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 在序列化通用类型时无法转换为 pojo.class。请协助。

这是为 Json fileReader 类编写的代码。

public class ReadJsonFile<T>  {

    private static final Object ReadJsonFile = null;
    public static void main (String[] args)
    {
        ReadJsonFile read  = new ReadJsonFile();
        read.getjsondata("src/test/resource/testdataresource/addplacerequest.json", Addbook.class);
    }
    public   List<T> getjsondata(String filepath, Class<T> typeofT)
    {

         //filepath= "src/test/resource/testdataresource/addplacerequest.json";
        Gson gson = new Gson(); ;
        BufferedReader bufferReader = null;
try {

            bufferReader = new BufferedReader(new FileReader(filepath));


         */
            Type ListType = new TypeToken<ArrayList<T>>(){}.getType();
            //gson.toJson(ReadJsonFile,ListType);
             List<T> arraylist = gson.fromJson(bufferReader, ListType); 
            Iterator iter = arraylist.iterator();
              while (iter.hasNext()) {
               System.out.println(iter.next());
              }
            return(arraylist);

    }catch(FileNotFoundException e) {
    throw new RuntimeException("Json file not found at path : " + filepath);
}finally {
    try { if(bufferReader != null) bufferReader.close();}
    catch (IOException ignore) {}
}
    }


}

我用过的POJO类是:

public class Addbook {


    public String name;
    public String isbn;
    public String aisle;
    public String author;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public String getAisle() {
        return aisle;
    }
    public void setAisle(String aisle) {
        this.aisle = aisle;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Addbook [name="+ name + ",isbn="+ isbn +",aisle="+ aisle +",authur="+ author +"]";
    }

 }

文件和 Pojo 阅读器类

public class TestDataBuild {


    private static String isbn;

    public static List<Addbook> addbook()

    {
            ReadJsonFile readJson=new ReadJsonFile();
    List<Addbook> addbook = readJson.getjsondata("src/test/resource/testdataresource/addplacerequest.json", Addbook.class);
    //System.out.println(addbook.getClass().getName()); // variable class name
    addbook.get(0).setName("name");
    addbook.get(1).setAuthor("author");//listOfClients.get(clientIndex).setFirstName(newFirstName);
    addbook.get(2).setIsbn(isbn);
    addbook.get(3).setAisle("aisle");

    return addbook;



        }

    }

使用的Json文件:

[
{
        "name": "The GoodDay",
        "isbn": 67086,
        "aisle": "GoodDay",
        "author": "fghwsdf"
},
{
        "name": "The BadDay",
        "isbn": 56897,
        "aisle": "BadDay",
        "author": "dhdjfjj"
}
]

我应该如何解决这个错误?

【问题讨论】:

  • getjsondata 中使用typeofT 的地方?
  • @EklavyaList addbook = readJson.getjsondata("src/test/resource/testdataresource/addplacerequest.json", Addbook.class);
  • 我说的是getjsondata方法里面
  • 对不起,它的类型不是 TypeofT@Eklavya

标签: java gson rest-assured-jsonpath


【解决方案1】:

代替

  Type ListType = new TypeToken<ArrayList<T>>(){}.getType();

使用

  Type ListType =TypeToken.getParameterized(ArrayList.class, typeofT).getType();

自 Gson 2.8.0 起支持

这个问题已经被问及并回答了 Gson TypeToken with dynamic ArrayList item type

【讨论】:

  • 我想我没有那个权限。
  • 没有抓住重点。然后解释问题并提出你的解决方案的一些细节
  • 请检查类似的另一个stackoverflow.com/questions/27253555/…标记为重复。
  • @DarshanaAfter update 建议面临以下问题“java.lang.NoSuchMethodError: com.google.gson.reflect.TypeToken.getParameterized(Ljava/lang/reflect/Type;[Ljava/lang/reflect/Type ;)Lcom/google/gson/reflect/TypeToken。”
  • 你使用的是哪个版本的 Gson?
猜你喜欢
  • 2020-05-03
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 2023-02-02
  • 2016-06-30
相关资源
最近更新 更多