配置Jackson以反序列化单引号(无效)JSON
我是使用杰克逊图书馆的新手。
我正在尝试执行此操作(请参见下文),并且抛出错误。
String x="{'candidateId':'k','candEducationId':1,'activitiesSocieties':'Activities for cand1'}";ObjectMapper mapper = new ObjectMapper();
try {
JsonNode df=mapper.readValue(x,JsonNode.class);
int i=0;
} catch .....
例外:
org.codehaus.jackson.JsonParseException: Unexpected character (''' (code
39)): was expecting double-quote to start field name
at [Source: java.io.StringReader@1afd1810; line: 1, column: 3]
at
org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
如果我将单引号(’)替换为双引号(“),则同样的事情起作用。
回答:
它不是有效的JSON,但是您可以告诉Jackson允许它。这是如何做。
String x = "{'candidateId':'k','candEducationId':1,'activitiesSocieties':'Activities for cand1'}";ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
JsonNode df = mapper.readValue(x, JsonNode.class);
System.out.println(df.toString());
// output: {"candidateId":"k","candEducationId":1,"activitiesSocieties":"Activities for cand1"}
以上是 配置Jackson以反序列化单引号(无效)JSON 的全部内容, 来源链接: utcz.com/qa/410671.html