java - 每次将对象附加到 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}]
解决方案
模型本身似乎存在问题。持有 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());
推荐阅读
- regex - 使用自定义公式连接动态开始行和结束行之间的单元格
- python - 没有使用scrapy的内部方法调用
- mysql - 我不明白 Laravel 中的功能(Builder $query)是如何工作的
- c# - 滚动数据网格
- db2 - 使用 DB2 检索不区分大小写的 SQL 选择查询
- c# - 我无法在 Raspberry Pi 上使用 Monodevelop 构建 C# 代码
- javascript - 如何从 df ~ 提取数据到我的网站上?
- angular - 如何以角度格式化来自数据库的日期
- node.js - 如何使用 nodeJS 通过 Aws Lambda 评级对数据进行排序
- php - 使用 MySQLi 的动态表名和准备好的语句