javascript
java string 反序列化-凯发k8官方网
我正在尝试使用简单的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 我也要避免这种情况。有没有简单的方法?
总结
以上是凯发k8官方网为你收集整理的java string 反序列化_如何将java.lang.string的空白json字符串值反序列化为null?的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: