Java 使用 Gson 将对象值更新到 json 文件

Java update object value to json file using Gson

我有以下 JSON 文件:

{
  "btnsAssign": [
    {
      "btnCode": 1,
      "btnItemTXT": "Baguette",
      "btnItemCode": 1001,
      "btnAvatarPath": "path"
    },
    {
      "btnCode": 2,
      "btnItemTXT": "Petit Pain",
      "btnItemCode": 1002,
      "btnAvatarPath": "path"
    }
  ]
}

我有以下 class :

BtnMenuAssignModel.java

public class BtnMenuAssignModel {
    @SerializedName("btnsAssign")
    @Expose
    private List<BtnsAssign> btnsAssign = null;
    public List<BtnsAssign> getBtnsAssign() {
        return btnsAssign;
    }
    public void setBtnsAssign(List<BtnsAssign> btnsAssign) {
        this.btnsAssign = btnsAssign;
    }
}

BtnsAssign.java

public class BtnsAssign {
    @SerializedName("btnCode")
    @Expose
    private Integer btnCode;
    @SerializedName("btnItemTXT")
    @Expose
    private String btnItemTXT;
    @SerializedName("btnItemCode")
    @Expose
    private Integer btnItemCode;
    @SerializedName("btnAvatarPath")
    @Expose
    private String btnAvatarPath;

    public Integer getBtnCode() {
        return btnCode;
    }

    public void setBtnCode(Integer btnCode) {
        this.btnCode = btnCode;
    }

    public String getBtnItemTXT() {
        return btnItemTXT;
    }

    public void setBtnItemTXT(String btnItemTXT) {
        this.btnItemTXT = btnItemTXT;
    }

    public Integer getBtnItemCode() {
        return btnItemCode;
    }

    public void setBtnItemCode(Integer btnItemCode) {
        this.btnItemCode = btnItemCode;
    }

    public String getBtnAvatarPath() {
        return btnAvatarPath;
    }

    public void setBtnAvatarPath(String btnAvatarPath) {
        this.btnAvatarPath = btnAvatarPath;
    }
}

我需要更新一些对象,例如:对象 btnItemTXT 索引 1 从 "Petit Pain" 到 "Pain Complet",我该怎么做?

首先将JSON文件转换为BtnMenuAssignModel然后修改BtnMenuAssignModel并将BtnMenuAssignModel转换为JSON文件:


Gson gson = new Gson();

// read initial json from jsonfile.json
FileReader reader = new FileReader(new File("D:\codes\gitlab\jsonfile.json"));
BtnMenuAssignModel newModel = gson.fromJson(reader, BtnMenuAssignModel.class);

// modify the json object
newModel.getBtnsAssign().forEach(btnsAssign -> {
    if (btnsAssign.getBtnCode() == 2) {
        btnsAssign.setBtnItemTXT("Pain Complet");
    }
});

// write new json string into jsonfile1.json file
File jsonFile = new File("D:\codes\gitlab\jsonfile1.json");
OutputStream outputStream = new FileOutputStream(jsonFile);
outputStream.write(gson.toJson(newModel).getBytes());
outputStream.flush(); 


这是适合我的代码:

String file = "c:/Users/QAXX2121/Documents/a.json";
    try {
        Gson gson = new Gson();
        // read initial json from jsonfile.json
        FileReader reader = new FileReader(new File(file));
        BtnMenuAssignModel newModel = gson.fromJson(reader, BtnMenuAssignModel.class);

        // modify the json object
        newModel.getBtnsAssign().forEach(btnsAssign -> {
            if (btnsAssign.getBtnCode() == 2) {
                btnsAssign.setBtnItemTXT("Taher");
            }
        });

        // write new json string into jsonfile1.json file
        File jsonFile = new File(file);
        OutputStream outputStream = new FileOutputStream(jsonFile);
        outputStream.write(gson.toJson(newModel).getBytes());
        outputStream.flush();