【发布时间】:2011-05-05 20:53:14
【问题描述】:
似乎无法弄清楚这一点。
我正在尝试在 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])
但a 失败,JSON 被转义并仅解析为JsonString,并且
b 返回一个空的 JsonObject。
有什么想法吗?
【问题讨论】:
-
提防 gson 验证陷阱:stackoverflow.com/questions/43233898/…