SaDeGH_F
SaDeGH_F

Reputation: 481

how to change json object name (key)?

Just a quick question:

If I have a json object which contains "key/value"s like this:

"name":"value"

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

Upvotes: 4

Views: 17392

Answers (2)

njzk2
njzk2

Reputation: 39406

Proper and compact implementation, given that jsonObject is the json object you are referring to and it is an instance of JSONObject:

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

Upvotes: 13

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

Create a new object with the new value

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

Upvotes: 0

Related Questions