作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有这个 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,因此它将被视为字符串。
最佳答案
我发现了一种更简单的方法。你可以只添加到你的 dto 下一个 setter :
public void setCustom(JsonNode custom) { this.custom = custom.toString(); }
为了得到简单的字符串,用 @JsonRawValue
注释它
关于json - 如何解析 json 但将一个特定节点序列化为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438833/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!