如何使用GSON解析嵌套映射

时间:2014-08-06 15:06:05

标签: json types map gson

我正在尝试使用以下格式解析JSON字符串:

  { 
      "request" : { 
          "Format" : "json",
          "Method" : "method",
          "NetworkId" : "net",
          "NetworkToken" : "token",
          "Service" : "service",
          "Target" : "target",
          "Version" : "2"
      },
      "response" : { 
          "data" : { 
              "1315" : { 
                  "AffiliateUser" : { 
                      "id" : "1315" 
                  } 
              } 
          },
          "errorMessage" : null,
          "errors" : [  ],
          "httpStatus" : 200,
          "status" : 1
      }
}

使用此代码:

public class AffiliateIdResponse{
    private Map<String, Map<String, Map<String, Map<String, String>>>> data;

    public static AffiliateIdResponse fromJson(String jsonString) {
        Type mapType = new TypeToken<Map<String, Map<String, Map<String, Map<String, String>>>>>() {
        }.getType();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(mapType, new Gson().getAdapter(TypeToken.get(mapType)));
        return gsonBuilder.create().fromJson(jsonString, AffiliateIdResponse.class);
    }

    public static AffiliateIdResponse handleResponse(MyHttpResponse httpResponse) {
        AffiliateIdResponse response = null;
        if (httpResponse.getStatusCode() == MyHttpStatus.OK
                || httpResponse.getStatusCode() == MyHttpStatus.ACCEPTED
                || httpResponse.getStatusCode() == MyHttpStatus.CREATED) {
            response = fromJson(httpResponse.getBody());
        } else {
        response = new AffiliateIdResponse();
        }
        response.setHttpResponseData(httpResponse);
        return response;
    }
}

但名为data的字段始终为null。如果我改为做这样的事情:

public class AffiliateIdResponse{
    public static AffiliateIdResponse fromJson(String jsonString) {
        Type mapType = new TypeToken<Map<String, Map<String, Map<String, Map<String, String>>>>>() {
        }.getType();
        Map<String, Map<String, Map<String, Map<String, String>>>> map = null;
        map = new Gson().fromJson("{ \"data\": { \"1315\": { \"AffiliateUser\": {" +
                    "\"id\":\"1315\"}}}}", mapType);
    }

    public static AffiliateIdResponse handleResponse(MyHttpResponse httpResponse) {
        AffiliateIdResponse response = null;
        if (httpResponse.getStatusCode() == MyHttpStatus.OK
                || httpResponse.getStatusCode() == MyHttpStatus.ACCEPTED
                || httpResponse.getStatusCode() == MyHttpStatus.CREATED) {
            response = fromJson(httpResponse.getBody());
        } else {
        response = new AffiliateIdResponse();
        }
        response.setHttpResponseData(httpResponse);
        return response;
    }
}

名为map的值实际上将包含我想要的数据。使用第一种方法时,为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

在你的第二个例子中,你已经降低了&#34;可见度&#34;反序列化到由

表示的4层数据
  1. 数据
  2. 1315
  3. AffiliateUser
  4. ID
  5. 因此,反序列化过程将每个层作为键映射到键值对中,其值为下一层映射。记录到控制台的输出显示为

    {data={1315={AffiliateUser={id=1315}}}}
    

    在您的第一个示例中,没有任何迹象表明您要解析的json的可见性级别,因此我假设您正在传递整个字符串。因此,你没有被告知&#34;反序列化过程如何&#34; step&#34;通过你的实现结构。

    如果您传入整个json结构,则以下内容将提取信息

    class ResponseContainer {
    
        private Response response;
    
        class Response {
             private Map<String, Map<String, Map<String, String>>> data;
             private Integer status;
    
             public String toString(){
              return "{data : " + data.toString() +"}, status: " + status + "}";
             }
        }
    
        public ResponseContainer fromJson(String jsonString) {
            new Gson().fromJson(jsonString, ResponseContainer.class);
        }
    
        public String toString(){
            return response.toString();
        }
    } 
    

    输出:{data : {1315={AffiliateUser={id=1315}}}}, status: 1}

    如果这是自定义反序列化链的一部分,其中jsonString参数是较大数据结构的子序列,那么&#34;为什么&#34;很可能是由于反序列化过程的错位 - &gt;结构关系。