简体   繁体   中英

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade

i have a large scale project in my hands but i simulated the problem i'm struggling with in this example:

my first class:

package asd;

import java.io.Serializable;

public class Grade implements Serializable{
String name;
Integer score;

public String getName() {
    return name;
}

public Grade() {}

public Grade(String name, Integer score) {
    this.name = name;
    this.score = score;
}

public void setName(String name) {
    this.name = name;
}

public Integer getScore() {
    return score;
}

public void setScore(Integer score) {
    this.score = score;
}
}

my second class:

package asd;

import java.io.Serializable;


public class Student implements Serializable {
private String name;
private Object grade;

public String getName() {
    return name;
}

public Student(String name, Object grade) {
    this.name = name;
    Grade = grade;
}

public Student() {}

public void setName(String name) {
    this.name = name;
}

public Object getGrade() {
    return Grade;
}

public void setGrade(Object grade) {
    Grade = grade;
}
}

and this is my main class:

package asd;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.*;

public class Test {
public static void main(String[] args){
    Student s1 = new Student();
    s1.setName("JeanPierre");
    s1.setGrade(new Grade("Math", 8));

    Gson gson = new GsonBuilder().create();
    String convertedToJson = gson.toJson(s1);
    System.out.println("Json string: " + convertedToJson);

    Student s2 = gson.fromJson(convertedToJson, Student.class);

    System.out.println("Student Name: " + s2.getName());
    System.out.println("Grade Name: " + ((Grade)s2.getGrade()).getName());
    System.out.println("Grade Score: " + ((Grade)s2.getGrade()).getScore());

}
}

Output:

Json string is :

{"name":"JeanPierre","Grade":{"name":"Math","score":8}}

Student Name: JeanPierre


Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade
    at asd.Test.main(Test.java:24)

My problem is when i call:

System.out.println("Grade Name: " + ((Grade)s2.getGrade()).getName());

or

System.out.println("Grade Score: " + ((Grade)s2.getGrade()).getScore());

i get this exception:

Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade
    at asd.Test.main(Test.java:24)

Declaration and setting of the Grade in Student is syntactically wrong. Not sure how it's even building like that.

public class Student implements Serializable
{
    protected String name ;
    protected Grade grade ;

    public Student( String name, Grade grade )
    {
       this.setName(name).setGrade(grade) ;
    }

    public String getName()
    { return this.name ; }

    public Student setName( String name )
    {
        this.name = name ;
        return this ;
    }

    public Grade getGrade()
    { return this.grade ; }

    public Student setGrade( Grade grade )
    {
        this.grade = grade ;
        return this ;
    }
}

You need to parse the grade class as well. It won't convert the entire complex object in one attempt. Try the below code:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Test {
public static void main(String[] args){
    Student s1 = new Student();
    s1.setName("JeanPierre");
    s1.setGrade(new Grade("Math", 8));

    Gson gson = new GsonBuilder().create();
    String convertedToJson = gson.toJson(s1);
    System.out.println("Json string: " + convertedToJson);

    Student s2 = gson.fromJson(convertedToJson, Student.class);

    System.out.println("Student Name: " + s2.getName());

    Grade g = gson.fromJson(s2.getGrade().toString(), Grade.class);
    System.out.println("Grade Name: " + g.getName());
    System.out.println("Grade Score: " + g.getScore());

}
}

Here s2.getGrade().toString() is still a valid JSON string. You are converting that to Grade class and using it. This is the right way to parse complex objects. Hope you understood.

I changed everything to XML and using XStream now, which works on my tests. Thanks for everyone's answers.

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