[英]deserializing the content of a json array in a class shows null values using gson
I am trying to deserialize the content of a json array with name topics.我正在尝试反序列化具有名称主题的 json 数组的内容。 after deserialing into the pojo, the topics array keeps printing null even though everything seems fine
反序列化到 pojo 后,主题数组继续打印 null 即使一切看起来都很好
//entry model //入口model
public class MainRequest {
private String bookId;
private List<topics> topics; //called as a list based on the json structure
}
//topics model //主题 model
public class topics {
private String DESCRIPTION;
private String INDEX;
}
the two models is executed in a controller this way这两个模型以这种方式在 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
.......
how can I print the value of the topics is the challenge如何打印主题的价值是挑战
[{
"bookId":"155",
"topics":[
{
"DESCRIPTION":"Little Beginnings",
"INDEX":"3455"
},
{
"DESCRIPTION":"Nostalgic moments",
"INDEX":"1234567"
}
],
Change the variable names of class topic to lower case and add the SerializedName
annotation将 class 主题的变量名称更改为小写并添加
SerializedName
注释
@Data
public class topics {
@SerializedName("DESCRIPTION")
private String description;
@SerializedName("INDEX")
private String index;
}
This should solve your problem.这应该可以解决您的问题。 BTW: especialy at this point it is important to follow the java naming convetions
顺便说一句:特别是在这一点上,遵循 java 命名约定很重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.