我们有这个Json:
{
"id": 500,
"field1": "TESTE",
"banco": {
"id": 300,
"descricao": "BANCO_TESTE"
},
"categorias": [
{
"id": 300,
"descricao": "PT",
"publica": true
}
]
}
我的豆子:
public class Asking implements Serializable {
private long id;
private String field1;
private Bank bank;
private List<Categoria> categorias;
//[getters and setters]
}
豆子银行和分类:
public class Bank implements Serializable {
private Long code;
private Long id;
private String descricao;
//getters and setters
}
public class Categoria implements Serializable {
private Long code;
private Long id;
private String descricao;
private boolean marcada;
private boolean publica;
//getters and setters
}
我打电话的时候:
gson.fromJson(strJson, tokenType);
出现错误:
Method threw 'java.lang.StackOverflowError' exception.
有什么问题?
答案 0 :(得分:1)
我无法重现这个问题。这里有两件事是错的:
Asking
实例Asking
实例,然后将其传递给新类的构造函数。您在设置gson.fromJson
方法时遇到了意外情况。以下是我正在使用的非常有用的东西:
public static void parseJSON(String jsonString) {
Gson gsonParser = new Gson();
Type collectionType = new TypeToken<Asking>(){}.getType();
Asking gsonResponse = gsonParser.fromJson(jsonString, collectionType);
System.out.println(gsonResponse);
}
检查你的bean类定义是否有额外的字段,或者,如果没有,请尝试使你的反序列化匹配我的。