gpt4 book ai didi

java - gson.toJson(jObj) stackoverflowerror .TypeAdapters$25.write(TypeAdapters.java :704)

转载 作者:行者123 更新时间:2023-12-01 23:46:12 24 4
gpt4 key购买 nike

我正在尝试使用 gson 将 json 对象转换为字符串。下面是代码

List<Student> studs = //from db
int count = studs.size();
Integer tot_pages= count/limit;
if(page>tot_pages){
page=tot_pages;
}
Integer start=limit*page-limit;
Gson gson = new Gson();
JsonObject jObj= new JsonObject();
jObj.addProperty("page", page);
jObj.addProperty("total",tot_pages);
jObj.addProperty("records",count);
JsonArray cell = new JsonArray();
JsonArray rows = new JsonArray();
JsonObject row= new JsonObject();
for(Student stud: studs){
row= new JsonObject();
row.addProperty("id", stud.getId());
cell=new JsonArray();
JsonPrimitive name = new JsonPrimitive(stud.getName());
JsonPrimitive rollno = new JsonPrimitive(stud.getRollno());
JsonPrimitive yr = new JsonPrimitive(stud.getYr());
cell.add(name);
cell.add(rollno);
cell.add(yr);
row.add("cell", row);
rows.add(row);
System.out.println(stud.getId());
}
System.out.println("done");

jObj.add("rows", rows);
System.out.println("done1");
return gson.toJson(jObj);

并出现以下错误

Caused by: java.lang.StackOverflowError at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:416) at java.lang.StringBuffer.append(StringBuffer.java:237) at java.io.StringWriter.write(StringWriter.java:101) at com.google.gson.stream.JsonWriter.string(JsonWriter.java:534) at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402) at com.google.gson.stream.JsonWriter.value(JsonWriter.java:495) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:686) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:704) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:704) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:704) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:704) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:704) at com.google.gson.internal.bind.TypeAdapters$25.write(TypeAdapters.java:704) <-----this line is repeated many times.

请有人帮助我。

最佳答案

更改:

row.add("cell", row);

致:

row.add("cell", cell);

顺便说一句,Gson 对象非常强大,并且应该能够序列化您的学生列表,而无需您现在使用的所有手动代码。最坏的情况是,您应该考虑创建一个 TypeAdapter为您的学生类(class),将其注册到 Gson,并序列化为列表:

假设的学生类(class):

public class Student implements Serializable {
private static final long serialVersionUID = 1L;

private String ssn;
private String schoolId;
private String firstName;
private String middleName;
private String lastName;
private Calendar enrollDate;

// Constructors, getters/setters

@Override
public String toString() {
return String.format("Student[%s,%s (%s)]", getLastName(),
getFirstName(), getSchoolId());
}
}

对应的适配器:

public class StudentAdapter extends TypeAdapter<Student> {

public StudentAdapter() {
super();
}

@Override
public Student read(final JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}

final Student student = new Student();

reader.beginObject();
while (reader.hasNext()) {
final String name = reader.nextName();
if ("firstName".equals(name)) {
student.setFirstName(reader.nextString());
} else if ("lastName".equals(name)) {
student.setLastName(reader.nextString());
} else if ("schoolId".equals(name)) {
student.setSchoolId(reader.nextString());
}
}
reader.endObject();

return student;
}

@Override
public void write(final JsonWriter writer, final Student student)
throws IOException {
if (student == null) {
writer.nullValue();
return;
}

writer.beginObject();
writer.name("firstName");
writer.value(student.getFirstName());
writer.name("lastName");
writer.value(student.getLastName());
writer.name("schoolId");
writer.value(student.getSchoolId());
writer.endObject();
}
}

测试代码:

final List<Student> origStudentList = Arrays.asList(new Student(
"B12J9", "Samantha", "Cole"), new Student("A09K2", "Adebisi",
"Onihan"), new Student(null, "Miguel", "Rodriguez"));

final Gson gson = new GsonBuilder().registerTypeAdapter(Student.class,
new StudentAdapter()).create();

final String json = gson.toJson(origStudentList);
System.out.println(json);

final List<Student> unmarshalledStudentList = gson.fromJson(json,
new TypeToken<List<Student>>() {
}.getType());
System.out.println(unmarshalledStudentList);

在上面的示例中,我演示了如何创建一个适配器,该适配器有选择地序列化以下属性的 schoolIdfirstNamelastName 属性:假设的 Student 对象。您可以将此作为起点,并且我链接的 Javadoc 中还有其他类型适配器的示例。

关于java - gson.toJson(jObj) stackoverflowerror .TypeAdapters$25.write(TypeAdapters.java :704),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17010405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com