我正在使用Jackson进行JSON序列化对象列表.
这是我得到的:
{"ArrayList":[{"id":1,"name":"test name"}]}
Run Code Online (Sandbox Code Playgroud)
但我想要这个:
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
Run Code Online (Sandbox Code Playgroud)
以下是我对此的处理方法:
接口:
public interface MyInterface {
public long getId();
public String getName();
}
Run Code Online (Sandbox Code Playgroud)
实施班:
@JsonRootName(value = "rootname")
public class MyImpl implements MyInterface {
private final long id;
private String name;
public MyImpl(final long id,final name) {
this.id = id;
this.name = name;
}
// getters
}
Run Code Online (Sandbox Code Playgroud)
JSon序列化:
public class MySerializer {
public static String serializeList(final List<MyInterface> lists) {
//check for null value.Throw …Run Code Online (Sandbox Code Playgroud) 我正在使用spring-boot 1.3.3并试图弄清楚如何在JSON序列化上拥有根名称.例如,我想......
{ stores: [
{
id: 1,
name: "Store1"
},
{
id: 2,
name: "Store2"
}]
}
Run Code Online (Sandbox Code Playgroud)
但相反,我得到了
[
{
id: 1,
name: "Store1"
},
{
id: 2,
name: "Store2"
}
]
Run Code Online (Sandbox Code Playgroud)
我一直在寻找@JsonRootName和定制Jackson2ObjectMapperBuilder配置但无济于事.在Grails中,这对于Json Views来说非常简单,我也试图看看它是如何直接转换为spring-boot但仍然无法弄明白.
我意识到这与这个问题类似,但我觉得在Spring(boot)的上下文中,它可能会以不同的方式应用,并且想知道如何.