gpt4 book ai didi

java - 配置 Jackson 以反序列化单引号(无效)JSON

转载 作者:IT老高 更新时间:2023-10-28 21:09:52 30 4
gpt4 key购买 nike

我是使用 jackson 库的新手。

我正在尝试这样做 [见下文],但它正在抛出错误。

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)

如果我用双引号(“)替换单引号('),同样的效果。

最佳答案

它不是有效的 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"}

关于java - 配置 Jackson 以反序列化单引号(无效)JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6591388/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com