使用 Gson 添加现有的 json 字符串
我有一个包含任意json的String对象。我想把它包装在另一个json对象中,就像这样:
{
version: 1,
content: >>arbitrary_json_string_object<<
}
如何可靠地将我的json字符串作为属性添加到其中,而无需手动构建它(即避免繁琐的字符串串联)?
class Wrapper {
int version = 1;
}
gson.toJson(new Wrapper())
// Then what?
请注意,添加的 json 不应进行转义,而应作为有效的 json 实体成为包装器的一部分,如下所示:
{
version: 1,
content: ["the content", {name:"from the String"}, "object"]
}
鉴于
String arbitraryJson = "[\"the content\", {name:\"from the String\"}, \"object\"]";