有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组Java使用Gson将对象值更新为json文件

我有以下JSON文件:

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

我有以下课程:

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;
    }
}

我需要更新一些对象,例如:objectbtnItemTXT索引1,从“小痛”到“痛全”,我怎么能


共 (2) 个答案

  1. # 1 楼答案

    首先将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(); 
    
    
    
  2. # 2 楼答案

    这是适合我的代码:

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