Java 分析JSON时出错(不匹配PutException)
解析JSON时遇到问题,错误如下:Java 分析JSON时出错(不匹配PutException),java,json,spring-boot,Java,Json,Spring Boot,解析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 这一条没有: { "st
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
这一条没有:
{
"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"
}
]
}
}
问题似乎是{
字段前面的{
字符,因为与[
一起工作。因此,这是我用来解析JSON的方法:
public Mono<PersonResponse> searchById(String id) {
return webClient.get().uri(id).retrieve().bodyToMono(PersonResponse.class);
}
问题不一定是JSON,而是JSON结构与您的
PersonResponse
类不匹配。PersonResponse中有一个info变量,它需要一个我假设为persons的数组,在第二个示例中,您试图将一个对象推到其中,但您不能。您必须更改JSON,也就是在本例中,您似乎不想要它,或者您试图将其解析到的类
您需要在
PersonResponse
中重新构造info变量,以匹配您试图解析的对象。将使用类编辑帖子,但我认为这不是问题所在,因为我在info中确实有一个包含人员和ID的对象的ArrayList。@用户如果您有一个列表,您希望如何使用JSON对象映射/反序列化到它?@DFSFOT如果你是对的,我更改了Info变量的结构,它工作了,谢谢你的时间。
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);
}
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
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