How to modify values of JsonObject / JsonArray directly?

Oh Chin Boon picture Oh Chin Boon · Jul 12, 2012 · Viewed 111.4k times · Source

Once i have parsed a JSON String into a GSON provided JsonObject class, (assume that i do not wish to parse it into any meaningful data objects, but strictly want to use JsonObject), how am i able to modify a field / value of a key directly?

I don't see an API that may help me.

https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonObject.html

Answer

Oh Chin Boon picture Oh Chin Boon · Jul 12, 2012

Strangely, the answer is to keep adding back the property. I was half expecting a setter method. :S

System.out.println("Before: " + obj.get("DebugLogId")); // original "02352"

obj.addProperty("DebugLogId", "YYY");

System.out.println("After: " + obj.get("DebugLogId")); // now "YYY"