Java 在Jackson中处理错误的Json属性值
我正在android项目中使用Jackson库 我有一节课Java 在Jackson中处理错误的Json属性值,java,android,parsing,jackson,Java,Android,Parsing,Jackson,我正在android项目中使用Jackson库 我有一节课 @JsonIgnoreProperties(ignoreUnknown = true) public class SomeResponse{ @JsonPropery("wiki") Wiki wiki; } @JsonIgnoreProperties(ignoreUnknown = true) public class Wiki{ @JsonProperty("title") String title; @JsonProperty(
@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
有没有办法处理属性值为空对象中的此错误?您可以使用正则表达式替换字符串,以替换特定的模式
您可以详细找到您使用的Jackson的哪个版本?我已经使用版本2.2.2测试了我的解决方案。在这个版本中,您可以将构造函数添加到接受
字符串参数的Wiki
类。
您的POJO类应该如下所示:
@JsonIgnoreProperties(ignoreUnknown = true)
class Wiki {
public Wiki() {
}
public Wiki(String title) {
this.title = title;
}
@JsonProperty("title")
private String title;
@JsonProperty("description")
private String description;
// getters, setters, toString
}
用法示例:
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
SomeResponse response = mapper.readValue(json, SomeResponse.class);
System.out.println(response);
如果不想创建包含奇怪内容的对象,还可以为Wiki
类编写自定义反序列化程序
class WikiJsonDeserializer extends JsonDeserializer<Wiki> {
@Override
public Wiki deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonToken token = jp.getCurrentToken();
if (JsonToken.VALUE_STRING == token) {
return null;
}
return jp.readValueAs(Wiki.class);
}
}
String.replaceAll
接受一个正则表达式,您可以轻松使用它匹配任意数量的空格:
resultFromServer.replaceAll("\"\\s*\\\\n\\s*\"", "\"\"");
(\s
是空格的符号)。此外,包含
测试也不是很必要,但是如果您想保留它,您必须将其替换为regexp匹配项才能测试空格
使用自定义indexof方法对字符串进行正则表达式匹配
/**
* contains with regex
*
* @param pattern
* @param s
* @return
*/
public static boolean contains(Pattern pattern, String s) {
return indexOf(pattern, s) > -1;
}
/**
* indexOf with regex
*
* @param pattern
* @param s
* @return
*/
public static int indexOf(Pattern pattern, String s) {
Matcher matcher = pattern.matcher(s);
return matcher.find() ? matcher.start() : -1;
}
+1…你是对的。我做了同样的事情,只是在开头“\”\\s*\\\\n\\s*\\”。无论如何,非常感谢你,我会接受你的正确答案。
/**
* contains with regex
*
* @param pattern
* @param s
* @return
*/
public static boolean contains(Pattern pattern, String s) {
return indexOf(pattern, s) > -1;
}
/**
* indexOf with regex
*
* @param pattern
* @param s
* @return
*/
public static int indexOf(Pattern pattern, String s) {
Matcher matcher = pattern.matcher(s);
return matcher.find() ? matcher.start() : -1;
}