0

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?

1
  • Can you update the question with code where you are reading the json as pojo using GSON? Commented May 13, 2020 at 11:10

2 Answers 2

1

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


Sign up to request clarification or add additional context in comments.

7 Comments

I need to write the changes to json file
write new json string into file can help?
With above code you mentioned , How can I write to the file ?
I try this : 'FileInputStream fis = new FileInputStream(file); Gson gson = new Gson(); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); gson.toJson(newModel, bw);' but I got error : 'java.io.FileNotFoundException: c:\BeBaker\a.ddll (Access is denied)'
check your file exist or not?
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.