【发布时间】:2020-07-11 23:11:53
【问题描述】:
我正在序列化一个对象,我想为其设置一个根值。
这是我的课。
@JsonRootName(value = "user")
public class User {
@JsonProperty
private String name;
@JsonProperty
private int id;
public User(int id, String name) {
this.name = name;
this.id = id;
}
}
这就是我序列化它的方式:
public static void main(String[] args) throws JsonProcessingException {
User user = new User(1, "foobar");
ObjectMapper mapper = new ObjectMapper();
String serilizedValue = mapper.writeValueAsString(user);
System.out.println(serilizedValue);
}
但序列化后的值为:
{"name":"foobar","id":1}
当我希望在类定义中得到一个根 json 值时。
你能帮忙吗?
【问题讨论】: