最好的方法是将您发布的响应包装到一个类中。
{"id": 2, "result": {"FirstWorld": {"FirstValue": 5, ... }, "SecondWorld":{"FirstValue": 5, ....}}, "error":null }import java.util.Map;public class Response { private int id; private Map<String, Map<String, Integer>> result; private String error; public int getId() { return id; } public void setId(int id) { this.id = id; } public Map<String, Map<String, Integer>> getResult() { return result; } public void setResult(Map<String, Map<String, Integer>> result) { this.result = result; } public String getError() { return error; } public void setError(String error) { this.error = error; }}我已经对此进行了测试,并且运行良好。
static String json = "{"id":2,"result":{"FirstWorld":{"FirstValue":5,"SecondValue":6},"SecondWorld":{"FirstValue":5,"SecondValue":6}},"error":null}"; public static void main(String[] args) { Gson g= new Gson(); Response b=g.fromJson(json, Response.class ); System.out.println(g.toJson(b)); }如果您需要
RegisterValues声明该类,则只需要
Map<String, Integer>用此类替换result,可能是extended
Map。



