gpt4 book ai didi

java - MismatchedInputException - Jackson 反序列化

转载 作者:行者123 更新时间:2023-12-02 09:57:23 27 4
gpt4 key购买 nike

我正在尝试读取我的 .json 文件。是一个 VehicleRepository 类。

"{\"vehicles\":[{\"id\":\"9467d079-4502-4dba-9d23-b8506dfc7ef4\",\"plate\":\"ghghghhg\",\"manufacturer\":\"Alfa Romeo\",\"model\":\"hgghgh\",\"color\":\"Amarelo\"}]}"

这是错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of VehicleRepository (although at least one Creator exists): Cannot construct instance of VehicleRepository (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value at [Source: (File); line: 1, column: 1]

要创建 .json 文件,我使用的是:

repository.vehicles = new ArrayList<Vehicle>();
repository.vehicles.add(vehicle);

json = mapper.writeValueAsString(repository);

ObjectMapper write = new ObjectMapper();
write.writeValue(new File("Database//Vehicles.json"), json);

要读取我正在使用的 .json 文件:

VehicleRepository newRepository = mapper.readValue(new File("Database\\Vehicles.json"), VehicleRepository.class);   

错误发生在上面的行中。

这是我的Vehicle 类:

public class Vehicle {

private String id;
private String plate;
private String manufacturer;
private String model;
private String color;

public Vehicle() {}

public Vehicle(String plate, String manufacturer, String model, String color) {
this.id = UUID.randomUUID().toString();
this.plate = plate;
this.manufacturer = manufacturer;
this.model = model;
this.color = color;
}

public Vehicle(String id, String plate, String manufacturer, String model, String color) {
this.id = id;
this.plate = plate;
this.manufacturer = manufacturer;
this.model = model;
this.color = color;
}

public String getId() {
return id;
}

public String getPlate() {
return plate;
}

public void setPlate(String plate) {
this.plate = plate;
}

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
}

这是我的 VehicleRepository 类:

public class VehicleRepository {

List<Vehicle> vehicles;

public VehicleRepository() {

}

public List<Vehicle> getVehicles() {
return vehicles;
}

public void setVehicles(List<Vehicle> vehicles) {
this.vehicles = vehicles;
}
}

有人可以帮助我吗?

最佳答案

代码中的问题是您首先将对象转换为字符串并将该字符串作为 JSON 写入文件。

您可以使用以下方法将对象写入文件:

write.writeValue(new File("Vehicles.json"), repository);

结果将是正确的 json:

{"vehicles":[{"id":"9467d079-4502-4dba-9d23-b8506dfc7ef4","plate":"ghghghhg","manufacturer":"Alfa Romeo","model":"hgghgh","color":"Amarelo"}]}

可以使用您已有的代码完美地阅读此内容:

VehicleRepository newRepository = mapper.readValue(new File("Vehicles.json"), VehicleRepository.class);

关于java - MismatchedInputException - Jackson 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com