【发布时间】:2013-11-12 11:25:40
【问题描述】:
我在我的 android 项目中使用 Jackson 库
我有课
@JsonIgnoreProperties(ignoreUnknown = true)
public class SomeResponse{
@JsonPropery("wiki")
Wiki wiki;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Wiki{
@JsonProperty("title")
String title;
@JsonProperty("description")
String description;
}
解析代码
String resultFromServer = ....;
ObjectMapper mapper = new ObjectMapper();
mapper.enable(
Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
Wiki str= mapper.readValue(resultFromServer,Wiki.class);
现在这段代码可以正常工作了 问题有时会像这样返回
{wiki:"\n "}
有时
{wiki:"\n"}
所以解析失败。我可以做到这一点
String resultFromServer = ....;
if (resultFromServer != null && resultFromServer.contains("\"\\\\n\"")) {
resultFromServer = resultFromServer.replaceAll("\"\\\\n\"", "\"\"");
}
现在这段代码处理了这种情况{wiki:"\n"}
但是第二种情况是不可预测的,因为"\n之后的空格数
有没有办法将属性值中的这个错误处理为空对象???
【问题讨论】:
标签: java android parsing jackson