我有一个 JSON 对象,它可能包含一些 null
值。我使用 ObjectMapper
来自 com.fasterxml.jackson.databind
将我的 JSON 对象转换为 String
。
private ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(object);
如果我的对象包含任何包含值为 null
的字段,则该字段不包含在来自 —fe33a55b4740b107d82a9c40cb1271-.e1– 的 String
writeValueAsString()
中我希望我的 ObjectMapper
给我 String
中的所有字段,即使它们的值为 null
。
例子:
object = {"name": "John", "id": 10}
json = {"name": "John", "id": 10}
object = {"name": "John", "id": null}
json = {"name": "John"}
原文由 LINGS 发布,翻译遵循 CC BY-SA 4.0 许可协议
Jackson 应该将
null
字段序列化为null
默认。看下面的例子印刷
您不需要做任何特别的事情。