如何解决解析 JSON 时出错 (MismatchedInputException)
我在解析 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",2021 3:49:27 AM"
},"info": {
"id": "1","person": [
{
"identifier": "John","role": "TEACHER"
},{
"identifier": "Homer",{
"identifier": "Michael",{
"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 实现了可序列化{
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
解决方法
问题不一定是 JSON,而是 JSON 结构与您的 PersonResponse
类不匹配。 PersonResponse 中有一个 info 变量,它需要一个我假设为人的数组,在第二个示例中,您试图将一个对象推入其中,但您不能。您必须更改您的 JSON(在这种情况下您似乎不想要),或者您尝试将其解析为的类。
您需要重构 PersonResponse
中的 info 变量以匹配您尝试解析的对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。