Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update elements in a JSONObject

Tags:

java

json

android

Lets say I gave a JSONObject

{  "person":{"name":"Sam", "surname":"ngonma"},  "car":{"make":"toyota", "model":"yaris"}  } 

How do I update some of the values in the JSONObject?

Like below :

String name = jsonArray.getJSONObject(0).getJSONObject("person").getString("name"); name = "Sammie"; 
like image 952
Harry Avatar asked Mar 01 '13 14:03

Harry


2 Answers

Use the put method: https://developer.android.com/reference/org/json/JSONObject.html

JSONObject person =  jsonArray.getJSONObject(0).getJSONObject("person"); person.put("name", "Sammie"); 
like image 195
cowls Avatar answered Oct 18 '22 05:10

cowls


Remove key and then add again the modified key, value pair as shown below :

    JSONObject js = new JSONObject();     js.put("name", "rai");      js.remove("name");     js.put("name", "abc"); 

I haven't used your example; but conceptually its same.

like image 32
rai.skumar Avatar answered Oct 18 '22 04:10

rai.skumar