【问题标题】:Error parsing JSON (MismatchedInputException)解析 JSON 时出错 (MismatchedInputException)
【发布时间】:2021-03-23 11:45:23
【问题描述】:

我在解析 JSON 时遇到问题,这是错误:

out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<packagename....>` out of START_OBJECT token

而且我知道它为什么会发生,但我只是不知道如何解决它。此 JSON 有效:

{
    "status_code": "SUCCESS",
    "time": {
        "date": "Mar 23, 2021 1:14:39 AM"
    },
    "info": [
        {
            "person": "2.2",
            "role": "TEACHER"
        },
        {
            "person": "2.3",
            "role": "TEACHER"
        }
    ]
}

这个没有:

{
    "status_code": "SUCCESS",
    "time": {
        "date": "Mar 23, 2021 3:49:27 AM"
    },
    "info": {
        "id": "1",
        "person": [
            {
                "identifier": "John",
                "role": "TEACHER"
            },
            {
                "identifier": "Homer",
                "role": "TEACHER"
            },
            {
                "identifier": "Michael",
                "role": "TEACHER"
            },
            {
                "identifier": "Sarah",
                "role": "TEACHER"
            }
        ]
    }
}

问题似乎是info 字段前面的{ 字符,因为[ 有效。所以这是我用来解析 JSON 的方法:

public Mono<PersonResponse> searchById(String id) {
        return webClient.get().uri(id).retrieve().bodyToMono(PersonResponse.class);
}

也试过了:

public Mono<PersonResponse[]> searchById(String id) {
            return webClient.get().uri(id).retrieve().bodyToMono(PersonResponse[].class);
}

在线错误:1,列:1。关于如何实现该方法的任何建议?

编辑:添加类。

人员响应:

public class PersonResponse implements Serializable{
        
    private static final long serialVersionUID = 7506229887182440471L;
        
    public String status_code;
    public Timestamp time;  
    public List<PersonDetails> info;

    public PersonResponse() {}

    ...getters / setters / toSting

人物详情:

公共类 PersonDetails 实现 Serializable{

private static final long serialVersionUID = 1294417456651475410L;

private int id;
private List<Person> person;

public PersonDetails(int version) {
    super();
    this.version = version;
}

...getters / setters / toSting

public class Person implements Serializable{
    
    private static final long serialVersionUID = 3290753964441709903L;
    
    private String identifier;
    private String role;
    
    public Person(String identifier, String role) {
        super();
        this.identifier = identifier;
        this.role = role;
    }

    ...getters / setters / toSting

【问题讨论】:

    标签: java json spring-boot


    【解决方案1】:

    问题不一定是 JSON,而是 JSON 结构与您的 PersonResponse 类不匹配。 PersonResponse 中有一个 info 变量,它需要一个我假设为人员的数组,在第二个示例中,您试图将一个对象推入其中,但您不能。您必须更改您的 JSON(在这种情况下您似乎不需要),或者您尝试将其解析为的类。

    您需要重构 PersonResponse 中的 info 变量以匹配您尝试解析的对象。

    【讨论】:

    • 打算用类编辑帖子,但我认为这不是问题,因为我确实有一个 ArrayList 人员和信息中的 ID 对象。
    • @user 如果你有一个列表,你希望 JSON 对象如何映射/反序列化到它?
    • @DFSFOT 你说得对,我改变了 Info 变量的结构,它起作用了,谢谢你的时间。
    猜你喜欢
    • 2021-11-13
    • 2019-09-16
    • 2020-05-30
    • 2021-01-25
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多