[英]deserializing the content of a json array in a class shows null values using gson
我正在嘗試反序列化具有名稱主題的 json 數組的內容。 反序列化到 pojo 后,主題數組繼續打印 null 即使一切看起來都很好
//入口model
public class MainRequest {
private String bookId;
private List<topics> topics; //called as a list based on the json structure
}
//主題 model
public class topics {
private String DESCRIPTION;
private String INDEX;
}
這兩個模型以這種方式在 controller 中執行
@PostMapping(path = "/do-library-request", consumes = "application/json")
public ResponseEntity<?> process(@RequestBody List<MainRequest> mainRequest,
HttpServletRequest request
) {
Gson gsons = new Gson();
logger.log(Level.INFO, "Request: {0}", gsons.toJson(mainRequest));
//out from logger
{"bookId":"155","topics":[{},{}]} // topics are null and this is the problem
.......
如何打印主題的價值是挑戰
[{
"bookId":"155",
"topics":[
{
"DESCRIPTION":"Little Beginnings",
"INDEX":"3455"
},
{
"DESCRIPTION":"Nostalgic moments",
"INDEX":"1234567"
}
],
將 class 主題的變量名稱更改為小寫並添加SerializedName
注釋
@Data
public class topics {
@SerializedName("DESCRIPTION")
private String description;
@SerializedName("INDEX")
private String index;
}
這應該可以解決您的問題。 順便說一句:特別是在這一點上,遵循 java 命名約定很重要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.