似乎无法弄清楚这一点。我正在尝试在 GSON 中进行 JSON 树操作,但在转换为 JsonObject
之前,我不知道或没有 POJO 可以将字符串转换为。有没有办法直接从 String
到 JsonObject
?
我尝试了以下(Scala 语法):
val gson = (new GsonBuilder).create
val a: JsonObject = gson.toJsonTree("""{ "a": "A", "b": true }""").getAsJsonObject
val b: JsonObject = gson.fromJson("""{ "a": "A", "b": true }""", classOf[JsonObject])
but a
fails, the JSON is escaped and parsed as a JsonString
only, and b
returns an empty JsonObject
.
有任何想法吗?
原文由 7zark7 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用 JsonParser;例如: