简体   繁体   中英

Java how to update a JSON object value and send it as a body to PUT request

I am trying to automate my REST service's PUT request and I am completely new to JAva. Here I am passing a PUT body(in JSON format) along with other headers. I had created a Java class for PUT in which I have getters and setters and I am assigning values using setter and creating my PUT body and sending this PUT body in my PUT request. My PUT body is something like this . I am able to update "id" and "name" and create my body object but I am not sure how do I update the "versionname" and"number" under "versions" path?

{
    "versions": [
        {
            "versionname": "Test",
            "number": 1

        }
    ],
    "id": 89960004,
    "name": "TEST CES LIST4",


}

The class for PUT body is as below:


public class putBody {

public String id;
public String name;
public String versionName;
public String number;


public String getId() {
    return id;
}

public void setName(String name) {
    this.name = name;
}
public String getName() {
    return name;
}

public String getversionName() {
    return versionName;
}

public void setversionName(String versionName) {
    this.versionName = versionName;
}

My question is how to update the "versionname" and "number" and create my putBody object. Any help much appreciated.

First, your PutBody class must have a collection to keep your version data, according to the JSON output that you provided.

For this purpose, just create a Version class that will be used for every single version information:

public class Version {

    public String versionName;
    public String number;

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

Then, here is your PutBody class should be look like

public class PutBody {

    public String id;
    public String name;
    public List<Version> versions;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Version> getVersions() {
        return versions;
    }

    public void setVersions(List<Version> versions) {
        this.versions = versions;
    }

}

And here is how to fill it with data according to your example values :

List<Version> versions = new ArrayList<Version>();
    Version version = new Version();
    version.setVersionName("Test");
    version.setNumber("1");
    versions.add(version);

    PutBody putBody = new PutBody();

    putBody.setName("TEST CES LIST4");
    putBody.setId("89960004");
    putBody.setVersions(versions);

And the JSON Output :

{
  "id": "89960004",
  "name": "TEST CES LIST4",
  "versions": [
    {
      "versionName": "Test",
      "number": "1"
    }
  ]
}

UPDATE

Google's Gson has great features for this purpose. You can easily convert Java instances to JSON and convert JSON strings back to Java instances.

Please check that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM