sample json to update
{
"student":{"name":"Rahul", "lastname":"sharma"},
"marks":{"maths":"88"}
}
To update the elements value in the json we need to assign the value and update.
try {
// Create a new instance of a JSONObject
final JSONObject object = new JSONObject(jsonString);
JSONObject studentJSON = object.getJSONObject("student");
studentJSON.put("name","Kumar");
object.remove("student");
object.put("student",studentJSON);
// Calling toString() on the JSONObject returns the JSON in string format.
final String json = object.toString();
} catch (JSONException e) {
Log.e(TAG, "Failed to create JSONObject", e);
}
updated value
{
"student":{"name":"Kumar", "lastname":"sharma"},
"marks":{"maths":"88"}
}