【发布时间】:2011-02-16 06:43:08
【问题描述】:
我正在从以 JSON 格式返回数据的服务器请求数据。在发出请求时将 HashMap 转换为 JSON 一点也不难,但另一种方式似乎有点棘手。 JSON 响应如下所示:
{
"header" : {
"alerts" : [
{
"AlertID" : "2",
"TSExpires" : null,
"Target" : "1",
"Text" : "woot",
"Type" : "1"
},
{
"AlertID" : "3",
"TSExpires" : null,
"Target" : "1",
"Text" : "woot",
"Type" : "1"
}
],
"session" : "0bc8d0835f93ac3ebbf11560b2c5be9a"
},
"result" : "4be26bc400d3c"
}
什么方法最容易访问这些数据?我正在使用 GSON 模块。
【问题讨论】:
-
Map<String,Object> result = new Gson().fromJson(json, Map.class);适用于 gson 2.6.2。 -
如何转换回来?我的意思是从 Map 到 Json Array。
标签: java json dictionary hashmap gson