Taher Balazzi
Taher Balazzi

Reputation: 1

Java update object value to json file using Gson

I have the following JSON file :

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

I have the below 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;
    }
}

I need to update some object E.G: object btnItemTXT index 1 from "Petit Pain" to "Pain Complet", How can I?

Upvotes: 0

Views: 2994

Answers (2)

Taher Balazzi
Taher Balazzi

Reputation: 1

This is the right code working for me :

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

Upvotes: 0

TongChen
TongChen

Reputation: 1430

First convert JSON file to BtnMenuAssignModel then modify BtnMenuAssignModel and convert BtnMenuAssignModel to JSON file:


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


Upvotes: 1

Related Questions