[英]Gson.fromJson() throws StackOverflowError
我們有這個傑森:
{
"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]
}
豆銀行和Categoria:
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.
怎么了?
我無法重現此問題。 這里有兩件事之一是錯誤的:
Asking
實例的字段 Asking
實例,然后將其傳遞到新類的構造函數中。 您使用gson.fromJson
方法的設置正在做意外的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類定義中是否包含其他字段,否則,請嘗試使反序列化與我的反序列化匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.