【问题标题】:Getting the com.fasterxml.jackson.core.JsonParseException while parsing [duplicate]解析时获取 com.fasterxml.jackson.core.JsonParseException [重复]
【发布时间】:2020-01-14 11:03:13
【问题描述】:

我的 json requestBody 是

{
    "action": "DONE",
    "email": {
        "29794": "sue@gmail.com",
        "29795": "sue@gmail.com"
    }
}

其中“电子邮件”是Map(key,value)pair,当我使用objectMapper.readvalue(entry.getValue(),List.class) 反序列化它时,出现以下异常:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'sue': was expecting ('true', 'false' or 'null')
 at [Source: (String)"sue@gmail.com"; line: 1, column: 4]

请帮忙看看哪里出错了。

【问题讨论】:

  • 放下完整的代码,我不明白你在做什么。

标签: java json spring parsing jsonparser


【解决方案1】:

对于这样一个简单的映射

HashMap<String,Object> result =
        new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);

(其中 JSON_SOURCE 是文件、输入流、阅读器或 json 内容字符串)

【讨论】:

  • List> emails = new ArrayList>();电子邮件 .add((new objectMapper().readValue(entry.getValue(), List.class));
【解决方案2】:

您的输入是对应于Map 而不是List 的Json 对象。所以改变你的代码

objectMapper.readvalue(entry.getValue(),List.class)

objectMapper.readvalue(entry.getValue(),Map.class)

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2014-10-10
    • 2019-12-03
    • 2020-06-29
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多