Essential Android JSON handling with org.json edit forum

Updating the elements in the JSON

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"}
}

Table Of Contents
7 JSON handling with org.json
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close