首页 > 解决方案 > 每次将对象附加到 JSON 时。创造了一个新的最高价值

问题描述

当我尝试接受用户输入并将列表添加到 JSON 文件时,它会添加一个新数组和一个未在任何地方定义的 all 数组。我有它附加,如果我没有,它会删除 JSON 中的任何先前信息。我见过 JSONObject 被使用,但 JSONObject 不只是 JSONPObject 可用。

我尝试了各种将用户输入写入 JSON 的方法,但它们都没有改变任何东西。

public static void main(String[] args) throws IOException {
    PrintWriter writer = null;
    ObjectMapper mapper = new ObjectMapper();
    List<Car> jsonList;
    jsonList = mapper.readValue(new File("cars.json"),
            new TypeReference<List<Car>>(){});
try{
    String make;
    String model;
    int year;
    String color;
    int miles;

    System.out.println("Input information you want to add");

    System.out.println("Input make");
    make = scan.nextLine();

    System.out.println("Input model");
    model = scan.nextLine();

    System.out.println("Input year");
    String userI2 = scan.nextLine();
    year = Integer.parseInt(userI2);

    System.out.println("Input color");
    color = scan.nextLine();

    System.out.println("Input miles");
    userI2 = scan.nextLine();
    miles = Integer.parseInt(userI2);

    Car cars = new Car();
    cars.setmake(make);
    cars.setmodel(model);
    cars.setcolor(color);
    cars.setmiles(miles);
    cars.setyear(year);

    carList.add(cars);

    String jsonCarList = mapper.writeValueAsString(carList);

    System.out.println(jsonCarList);

    writer = new PrintWriter(new FileWriter("cars.json", true));

    writer.println(jsonCarList);
}catch (FileNotFoundException | JsonProcessingException e){
    System.out.println(e.getMessage());

}catch (IOException e){
    System.out.println(e.getMessage());
}finally {
    if (writer != null) {
        writer.flush();
        writer.close();
    }

车号:

private String make;
    private String model;
    private int year;
    private String color;
    private int miles;

    public Car(String make, String model, int year, String color, int miles) {
        this.make = make;
        this.model = model;
        this.year = year;
        this.color = color;
        this.miles = miles;
    }

private List<Car> carList = new ArrayList<Car>();

    public Car() {

    }

    public void add(Car newCar){

        carList.add(newCar);
    }

    public String getmake() {
        return make;
    }

    public void setmake(String make) {
        this.make = make;
    }

    public String getmodel() {
        return model;
    }

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

    public int getyear() {
        return year;
    }

    public void setyear(int year) {
        this.year = year;
    }

    public String getcolor() {
        return color;
    }

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

    public int getmiles() {
        return miles;
    }

    public void setmiles(int miles) {
        this.miles = miles;
    }

这将创建一个如下所示的 JSON 文件:

[{"make":"toyota","model":"camry","year":2011,"color":"gray","miles":90000,"all":[]}]
[{"make":"nissan","model":"rogue","year":1093,"color":"red","miles":49040,"all":[]}]

代替

[{"make":"toyota","model":"camry","year":2011,"color":"gray","miles":90000},
{"make":"nissan","model":"rogue","year":1093,"color":"red","miles":49040}]

标签: javajsonjackson-databind

解决方案


模型本身似乎存在问题。持有 Car 列表的 Car 本身没有任何意义。更好的方法是编写另一个类 CarInventory,其中包含 Car 对象列表并从 Car 类中删除“carList”

public class CarInventory {

    private List<Car> carList;

    public List<Car> getCarList() {
        if(carList == null)
            carList = new ArrayList<>();
        return carList;
    }

    public void setCarList(List<Car> carList) {
        this.carList = carList;
    }

    public void addCar(Car car) {
        getCarList().add(car);
    }
}

然后在你的主要修改读取到

CarInventory inventory = mapper.readValue(new File("cars.json"),
                    CarInventory.class);

读取用户输入后

Car car = new Car();
car.setmake(make);
car.setmodel(model);
car.setcolor(color);
car.setmiles(miles);
car.setyear(year);

inventory.addCar(car);

String inventory = mapper.writeValueAsString(inventory);

System.out.println(inventory);

或者您可以从库存中打印列表

String carList = mapper.writeValueAsString(inventory.getCarList());

推荐阅读