如何使用Jackson重命名JSON序列化中的根密钥 [英] How to rename root key in JSON serialization with Jackson
问题描述
我使用Jackson进行对象列表的JSON序列化。
I am using Jackson for JSON serialization of a list of objects.
这是我得到的:
{"ArrayList":[{"id":1,"name":"test name"}]}
但我希望如此:
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
以下是我的方法:
接口:
public interface MyInterface {
public long getId();
public String getName();
}
实施类:
@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
}
JSon序列化:
public class MySerializer {
public static String serializeList(final List<MyInterface> lists) {
//check for null value.Throw Exception
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
return mapper.writeValueAsString(lists);
}
}
测试:
final List<MyInterface> list = new ArrayList<MyImpl>();
MyImpl item = new MyImpl(1L,"test name");
list.add(item);
final String json = MySerializer.serializeList(list);
System.out.println(json);
这是我得到的:
{"ArrayList":[{"id":1,"name":"test name"}]}
但我想要这个:
{"rootname":[{"id":1,"name":"test name"}]} // ie showing the string I want as the root name.
我已经尝试了所有建议的解决方案,但我未能实现目标。我看过:
I have tried all suggested solutions I could find but failed to achieve my goal. I have looked at:
- Jackson : custom collection serialization to JSON
- How do I rename the root key of a JSON with Java Jackson?
- Jackson : custom collection serialization to JSON
或者我错过了什么?我正在使用杰克逊1.9.12。欢迎提供这方面的任何帮助。
Or am I missing something? I am using jackson 1.9.12 for this. Any help in this regard is welcome.
推荐答案
嗯,默认情况下,Jackson在尝试确定根名称时会使用两个注释之一显示包装值 - @XmlRootElement
或 @JsonRootName
。它希望这个注释在被序列化的类型上,否则它将使用简单名称作为根名称的类型。
Well, by default Jackson uses one of two annotations when trying to determine the root name to be displayed for wrapped values - @XmlRootElement
or @JsonRootName
. It expects this annotation to be on the type being serialized, else it will use the simple name of the type as the root name.
在您的情况下,您正在序列化一个列表,这就是根名称的原因是'ArrayList'(被序列化的类型的简单名称)。列表中的每个元素都可以是使用@JsonRootName注释的类型,但列表本身 不是 。
In your case, you are serializing a list, which is why the root name is 'ArrayList' (simple name of the type being serialized). Each element in the list may be of a type annotated with @JsonRootName, but the list itself is not.
当您尝试换行的根值是一个集合时,您需要一些方法来定义换行名称:
When the root value you are trying to wrap is a collection then you need some way of defining the wrap name:
您可以创建一个包装类来保存列表,并使用注释来定义所需的属性名称(您只需要当您无法直接控制 ObjectMapper
/ JSON转换过程时使用此方法:
You can create a wrapper class to hold the list, with an annotation to define the desired property name (you only need to use this method when you do not have direct control of the ObjectMapper
/JSON transformation process):
class MyInterfaceList {
@JsonProperty("rootname")
private List<MyInterface> list;
public List<MyInterface> getList() {
return list;
}
public void setList(List<MyInterface> list) {
this.list = list;
}
}
final List<MyInterface> lists = new ArrayList<MyInterface>(4);
lists.add(new MyImpl(1L, "test name"));
MyInterfaceList listHolder = new MyInterfaceList();
listHolder.setList(lists);
final String json = mapper.writeValueAsString(listHolder);
Object Writer
这是首选选项。使用配置的 ObjectWriter
生成JSON的实例。特别是,我们感兴趣的是 withRootName
方法:
Object Writer
This is the preferable option. Use a configured ObjectWriter
instance to generate the JSON. In particular, we are interested in the withRootName
method:
final List<MyInterface> lists = new ArrayList<MyInterface>(4);
lists.add(new MyImpl(1L, "test name"));
final ObjectWriter writer = mapper.writer().withRootName("rootName");
final String json = writer.writeValueAsString(lists);
这篇关于如何使用Jackson重命名JSON序列化中的根密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!