java 支持json单引号解析

Posted wegycx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 支持json单引号解析相关的知识,希望对你有一定的参考价值。

I am trying to do this [see below], and it is throwing error.

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 .....

Exception:

org.codehaus.jackson.JsonParseException: Unexpected character (‘‘‘ (code 39)): was expecting double-quote to start field name
at [Source: [email protected]; line: 1, column: 3]
  at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)

Solution:

It‘s not valid JSON, but you can tell Jackson to allow it. Here‘s how.

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"}

  

 

以上是关于java 支持json单引号解析的主要内容,如果未能解决你的问题,请参考以下文章

Java:想要解析JSON,但是遇到好多双引号,该如何处理

如何将带单引号的字符串转换为双引号以进行 json 解析

尝试使用转义单引号解析 JSON 时,Javascript 中出现“意外标识符”错误

JSON 的标准:双引号而非单引号!

带单引号和双引号的 JSON.parse 字符串?

带单引号和双引号的 JSON.parse 字符串?