获取对象中的对象
一:静态转换方法
1 public static <T> ApiResponse<T> parseResultV2(String json, Class<T> clazz) { 2 return JSONObject.parseObject(json, new TypeReference<ApiResponse<T>>(clazz) { 3 }); 4 }
二:实体类
1 package com.wing.ihome.cloud.system.modular.system.um.model; 2 3 import lombok.Data; 4 5 import java.util.Date; 6 7 @Data 8 public class ApiResponse<T> { 9 /** 10 * true代表调用错误 11 */ 12 private boolean error = false; 13 /** 14 * 错误代码 15 */ 16 private String code; 17 private String message; 18 private Date timestamp; 19 private T data; 20 21 }
三:使用
1 String json2 = "{\"error\":false,\"code\":\"00000\",\"message\":\"success\",\"timestamp\":\"2020-11-23T05:51:06.424+0000\",\"data\":\"ASeMYuoRU0DyF0gbdzhw7w==\"}"; 2 ApiResponse<String> stringApiResponse = parseResultV2(json2, String.class); 3 System.out.println(stringApiResponse.getData());