Modify json with GSON without using a POJO
gson, java, json
Solution
Here's the shortest I came up with.
JsonElement je = new Gson().fromJson(jsonString, JsonElement.class);
JsonObject jo = je.getAsJsonObject();
jo.add("key", value);
Once you have the JsonObject, gson has many methods to manipulate it.
Problem
I want to modify a json content without converting it into a POJO. I am using GSON Library. Following are the use case: ``` String jsonString = "[{\"key1\":\"Hello\",\"key2\":\"World\"},{\"key1\":\"Nice\",\"key2\":\"Town\"}]"; JsonElement jsonElement = gson.fromJson(jsonString, JsonElement.class); ``` Is there any way where I can set value of key1 to some value (let say "Test") in each array, without converting things into POJO