【发布时间】:2011-01-26 23:46:36
【问题描述】:
假设我有一个 pojo:
import org.codehaus.jackson.map.*;
public class MyPojo {
int id;
public int getId()
{ return this.id; }
public void setId(int id)
{ this.id = id; }
public static void main(String[] args) throws Exception {
MyPojo mp = new MyPojo();
mp.setId(4);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
System.out.println(mapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.WRAP_ROOT_VALUE));
System.out.println(mapper.writeValueAsString(mp));
}
}
当我使用 Jackson ObjectMapper 进行序列化时,我得到了
true
{"id":4}
但我想要
true
{"MyPojo":{"id":4}}
我已经到处搜索了,Jacksons 的文档确实杂乱无章,而且大多已经过时了。
【问题讨论】:
标签: java json serialization jackson