假设我有这个 JSON 示例:
{
"title" : "tttt",
"custom" : {
"a" : "aaaaa",
"b" : "bbbbb",
"c" : {
"d" : "dddd"
}
}
}
我想将其反序列化为以下类:
public class Article {
private String title;
private String custom;
public void setTitle(String title) { this.title = title; }
public String getTitle() { return title; }
public void setCustom(String custom) { this.custom = custom; }
public String getCustom() { return custom; }
}
我想要的结果是标题按正常反序列化,但“自定义”下的 json 子树不反序列化并设置为原始 json 字符串:
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(content, Article.class);
assertEquals(article.getTitle(), "tttt");
assertEquals(article.getCustom(),
"{\"a\" : \"aaaaa\"," +
"\"b\" : \"bbbbb\"," +
"\"c\" : {" +
"\"d\" : \"dddd\" " +
"}" +
"}");
另一个重要注意事项是,我无法将自定义节点下的原始 JSON 更改为使用转义 json,因此它将被视为字符串。