class com.google.gson.internal.LinkedTreeMap cannot be cast to class

99 Views Asked by At

I'm having troubles to retrieve an object from a json (using gson).

Here is the object's class attributs :

abstract class Table {
    protected ArrayList<ArrayList<Object>> table;
    protected String requete;

I put this object in a ArrayList then in a json like this :

Gson gson = new Gson();
ArrayList<Table> buffer = new ArrayList<Table>();
buffer.add(this);
try (FileWriter file = new FileWriter(nomTest)) {
            gson.toJson(buffer, file);

Then I try to retrieve the data :

Gson gson = new Gson();
ArrayList<Table> reponses = null;
FileReader reader = new FileReader(nomTest))
reponses = (ArrayList<Table>) gson.fromJson(reader, new ArrayList<Table>().getClass());
Table buffer = reponses.get(numQuestion-1); //ERROR CAST

But here I get

class com.google.gson.internal.LinkedTreeMap cannot be cast to class evaluateur.Table

I could not find a solution in my case.

Thanks for the help.

EDIT1 :

Here is the JSON (don't mind the 2 same objects) :

[
   {
      "table":[[2.0,"Alain","Verse"],[7.0,"Pierre","Kiroul"]],
      "requete":"SELECT * FROM test WHERE age \u003e 40"
   },
   {
      "table":[[2,"Alain","Verse"],[7,"Pierre","Kiroul"]],
      "requete":"SELECT * FROM test WHERE age \u003e 40"
   }
]

When i use System.out.println(reponses.get(numQuestion-1)); it gives me {table=[[2.0, Alain, Verse], [7.0, Pierre, Kiroul]], requete=SELECT * FROM test WHERE age > 40}

I'd like to retrieve these "table" and "requete".

0

There are 0 best solutions below