【发布时间】:2013-12-11 15:03:47
【问题描述】:
我有一个包含任意 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\"]";
【问题讨论】:
-
您是否尝试过添加
content字符串字段?我很想看看结果。 -
可能内容会被转义。
-
你的问题解决了吗?