简体   繁体   中英

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class

Getting java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class while serializing Generic type.Kindly assist.

This is the code written for Json fileReader class.

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 class that I have used is:

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 +"]";
    }

 }

File and Pojo reader class

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 file used:

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

How should I resolve this error?

Instead of

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

Use

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

Support since Gson 2.8.0

This question has been asked and answered Gson TypeToken with dynamic ArrayList item type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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