我手中有一个大型项目,但我在这个例子中模拟了我正在努力解决的问题:
我的第一堂课:
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;
}
}
我的第二堂课:
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;
}
}
这是我的主要课程:
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());
}
}
输出:
Json字符串是:
{"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)
我的问题是我打电话的时候:
System.out.println("Grade Name: " + ((Grade)s2.getGrade()).getName());
或
System.out.println("Grade Score: " + ((Grade)s2.getGrade()).getScore());
我得到了这个例外:
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)
答案 0 :(得分:1)
Grade
中Student
的声明和设置在语法上是错误的。不确定它是如何构建的那样。
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 ;
}
}
答案 1 :(得分:0)
您还需要解析成绩课程。它不会在一次尝试中转换整个复杂对象。请尝试以下代码:
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());
}
}
此处s2.getGrade().toString()
仍然是有效的JSON字符串。您正在将其转换为Grade类并使用它。这是解析复杂对象的正确方法。希望你能理解。
答案 2 :(得分:0)
我将所有内容更改为XML并立即使用XStream,这适用于我的测试。感谢大家的回答。