具有对象类型的 Jackson JSON 列表
2022-09-03 09:13:54
我必须从对象列表中序列化JSON。生成的 JSON 必须如下所示:
{
"status": "success",
"models": [
{
"model": {
"id": 23,
"color": "red"
}
},
{
"model": {
"id": 24,
"color": "green"
}
}
]
}
当我简单地序列化它时,我缺少类型/键“模型”:
List<Model> list = new ArrayList<Model>(); // add some new Model(...)
Response r = new Response("success", list); // Response has field "models"
相反,我只是得到这个:
{
"status": "success",
"models": [
{
"id": 23,
"color": "red"
},
{
"id": 24,
"color": "green"
}
]
}
如何为每个对象添加“模型”,而不必使用属性“model”编写一个愚蠢的包装类?
我的类如下所示:
public class Response {
private String status;
private List<Model> models;
// getters / setters
}
public class Model {
private Integer id;
private String color;
// getters / setters
}