How to change json object name (key)?

Just a quick question:

If I have a json object that contains a "key / value" like:

"name":"value"

      

and I want to change parts of the name to something else, what should I do? I don't want to delete and do it again.

+3


source to share


2 answers


Correct and compact implementation, given that jsonObject

- this is the json object you are accessing and this is an instance jsonObject

:



jsonObject.put("new name", jsonObject.remove("name"));

      

+9


source


Create a new object with a new value



new JSONObject(jsonobject.toString().replace("\"name\":","\"newvalue\":");)

      

0


source







All Articles