我正在向服务器请求数据,服务器以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 版本中使用。意思是将传入的 json 字符串转化成一个键值对集合(key为String类型,value为Object类型)并存储在result变量中。 - Ferran Maylinch