I have a String object containing some arbitrary json. I want to wrap it inside another json object, like this:
{
version: 1,
content: >>arbitrary_json_string_object<<
}
How can I reliably add my json string as an attribute to it without having to build it manually (ie avoiding tedious string concatenation)?
class Wrapper {
int version = 1;
}
gson.toJson(new Wrapper())
// Then what?
Note that the added json should not be escaped, but a be part of the wrapper as a valid json entity, like this:
{
version: 1,
content: ["the content", {name:"from the String"}, "object"]
}
given
String arbitraryJson = "[\"the content\", {name:\"from the String\"}, \"object\"]";
For those venturing on this topic, consider this.
A a = getYourAInstanceHere();
Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(a);
jsonElement.getAsJsonObject().addProperty("url_to_user", url);
return gson.toJson(jsonElement);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With