gpt4 book ai didi

java - 每次将对象附加到 JSON 时。创建了一个新的最高值

转载 作者:行者123 更新时间:2023-12-02 09:34:41 26 4
gpt4 key购买 nike

当我尝试获取用户输入并将列表添加到 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}]

最佳答案

模型本身似乎有问题。 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);
}
}

然后在你的main中修改读取为

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());

关于java - 每次将对象附加到 JSON 时。创建了一个新的最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57629828/

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