字符串转成 JSON 时的 '单引号' 问题 (jackson)

华宰 发布于 2011/07/06 13:18
阅读 34K+
收藏 2

代码:

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)

而当我把单引号换成双引号后,就OK。

 

加载中
2
鉴客
鉴客

要做一下特殊处理,看下面代码的第三行:

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"}
0
裸奔不是醉
裸奔不是醉
fastjson好像没这个问题,另外jackson会存在对象完全匹配要求,这点非常不人道啊
返回顶部
顶部