码迷,mamicode.com
首页 > 编程语言 > 详细

java 支持json单引号解析

时间:2018-03-11 19:29:20      阅读:1043      评论:0      收藏:0      [点我收藏+]

标签:AC   char   layout   expect   .json   gpo   ted   nod   and   

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: java.io.StringReader@1afd1810; 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单引号解析

标签:AC   char   layout   expect   .json   gpo   ted   nod   and   

原文地址:https://www.cnblogs.com/WegYcx/p/8544875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!