javascript
java string 反序列化_如何将java.lang.String的空白JSON字符串值反序列化为null?
我正在尝试使用简单的JSON反序列化为Java对象。不过,我,让空 字符串
值,java.lang.String属性值。在其余的属性中,空白值将转换为 空 值(这是我想要的)。
我的JSON和相关的Java类在下面列出。
JSON字串:
{
"eventId" : 1,
"title" : "sample event",
"location" : ""
}
EventBean 类POJO:
public class EventBean {
public Long eventId;
public String title;
public String location;
}
我的主要课程代码:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
try {
File file = new File(JsonTest.class.getClassLoader().getResource("event.txt").getFile());
JsonNode root = mapper.readTree(file);
// find out the applicationId
EventBean e = mapper.treeToValue(root, EventBean.class);
System.out.println("It is " + e.location);
}
我期待打印“它为空”。相反,我得到“ It is”。显然, 杰克逊 在转换为我的 String 对象类型时并未将空白String值视为NULL 。
我读到了预期的地方。但是,对于 java.lang.String 我也要避免这种情况。有没有简单的方法?
总结
以上是生活随笔为你收集整理的java string 反序列化_如何将java.lang.String的空白JSON字符串值反序列化为null?的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java grizzly_Grizzly
- 下一篇: springboot actuator_