To modify the values of a JSONObject
in Java, you can use the put
method. This method takes the key of the value you want to modify and the new value as arguments. For example:
JSONObject obj = new JSONObject();
obj.put("key", "value");
// To modify the value of "key"
obj.put("key", "new value");
You can also use the get
method to retrieve the value of a key, modify it, and then use the put
method to update the value in the JSONObject
. For example:
String oldValue = obj.getString("key");
String newValue = oldValue + " some additional text";
obj.put("key", newValue);
Keep in mind that the put
method can only be used to add or modify key-value pairs in a JSONObject
. It cannot be used to add or modify elements in a JSONArray
. To do that, you can use the put
method of the JSONArray
class.