【发布时间】:2014-06-03 19:38:37
【问题描述】:
我有一个 JSON 字符串打印为:
{
"result":"_error",
"invokeId":2,
"data":{
"timestamp":1.401824129758E12,
"rootCause":{
"message":"Wrong client version for server",
"localizedMessage":"Wrong client version for server",
"rootCauseClassname":"login.impl.ClientVersionMismatchException",
"substitutionArguments":[
"4.9",
"4.8.14_05_16_17_02"
],
"errorCode":"LOGIN-0001"
},
"headers":{
},
"correlationId":"00E36368-6158-CD63-56CA-4F39493E8ED3",
"faultCode":"Server.Processing",
"messageId":"D8424EAD-8D0B-5441-820B-775B99812CFD",
"faultString":"login.impl.ClientVersionMismatchException : Wrong client version for server",
"timeToLive":0.0,
"clientId":"D8424E8B-5F0F-1ECD-C29F-C6440488B0FC",
"destination":"loginService"
},
"version":0
}
我知道如何在 Python 中做到这一点,所以我会展示它。
让x 成为 Python 中的 JSON 对象作为字典。 x['data']['rootCause']['substitutionArguments'] 会得到我"4.8.14_05_16_17_02"。
如何在 Java 代码中获得"4.8.14_05_16_17_02"?我使用Gson 作为我的Java JSON 库。
【问题讨论】:
-
你是如何使用 Gson 的?
-
我使用 Gson 使用
toJson(...)函数打印我的对象。 -
所以你有一个对象层次结构。该层次结构中的
substitutionArguments在哪里?取回它。 -
我要问的是,如何将我的 JSON 字符串转换为 HashMap(相当于 Java 的字典)?
-
我要问的是,到目前为止,您为使用 Gson 检索该字段做了什么?向我们展示您的尝试。 Gson 与 POJO 一起用于与 JSON 之间的映射。您的 POJO 在哪里/什么?