【发布时间】:2021-03-31 06:52:38
【问题描述】:
我正在尝试在 Spring Boot 中将我在 JSON 中获得的响应存储在我的数据库中。但是当它显示JSON Parse Error
这是我的 JSON 响应
[
{
id: 1,
name: "Bilbo Baggins",
location: "india",
email: "jba2hba.com",
dateOfBirth: "2020-12-21T13:13:38.000+00:00"
},
{
id: 2,
name: "Frodo Baggins",
location: "bhutan",
email: "jhb@hbh.com",
dateOfBirth: "2020-12-21T13:13:38.000+00:00"
}
]
我的员工模型
@Entity
public class Employee {
private @Id @GeneratedValue( strategy = GenerationType.AUTO ) Long id;
private String name;
private String location;
private String email;
private Date dateOfBirth;
public Employee() {
}
public Employee(String name, String location, String email, Date date) {
this.name = name;
this.location = location;
this.email = email;
this.dateOfBirth = date;
}
//getters and setters
我的 getEmployees() 方法
public void getEmployees() {
RestTemplate restTemplate = new RestTemplate();
Employee result = restTemplate.getForObject(Constants.URI, Employee.class);
System.out.println(result);
}
这是我遇到的错误
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.nagarro.hrLogin.entity.Employee` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.11.3.jar:2.11.3]
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1468) ~[jackson-databind-2.11.3.jar:2.11.3]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1242) ~[jackson-databind-2.11.3.jar:2.11.3]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1190) ~[jackson-databind-2.11.3.jar:2.11.3]
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:604) ~[jackson-databind-2.11.3.jar:2.11.3]
//
如果有人可以建议我这样做会很有帮助,我无法弄清楚可能导致错误的原因
【问题讨论】:
-
似乎响应是员工列表/数组,而不是单个。
-
有没有办法循环遍历列表来获取单个员工?