Bootstrap

复杂json字符串反序列化为泛型对象

复杂json字符串反序列化为泛型对象

protected ApiRequest parseJson(String json, Class clazz){

     ObjectMapper om = new ObjectMapper();  //jacson包

     ApiRequest<T> obj = om.readValue(json, new TypeReference<ApiRequest<T>>(){});

     T body = om.readValue(om.writeValueAsString(obj.getBody()), clazz);

     obj.setBody(body);

     return obj;

}

public class ApiRequest{

   private SysHeadReq syshead;


   private T body;

}

调用方法:

ApiRequest apiRequest = parseJson(json, OpenAccount.class);

可以反序列化复杂json字符串到泛型对象。

;