Gson 解析响应以获得通用 class

Gson parse response to get generic class

我有回复class

public class ResponseModel<T> {
    private boolean isRequestSuccessful;

    public boolean getIsRequestSuccessful() {
        return this.isRequestSuccessful;
    }

    public void setIsRequestSuccessful(boolean isRequestSuccessful) {
        this.isRequestSuccessful = isRequestSuccessful;
    }

    private String message;

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private T object;

    public T getObject() {
        return this.object;
    }

    public void setObject(T object) {
        this.object = object;
    }
}

我的 API 将 return 键入 T。我想解析来自 API 的响应并创建 ResponseModel 类型的对象。

我正在尝试实现类似下面的内容,我可以使用 C# 轻松完成。请帮助 Java

如何做到这一点
public static ResponseModel<T> Get(String requestUri) throws ClientProtocolException,IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(requestUri);
    httpGet.addHeader("TenantKey", TenantKey);
    httpGet.addHeader("accept", "application/json");
    HttpResponse response = client.execute(httpGet);

    ResponseModel<T> responseModel = new ResponseModel<T>();

    if (response.getStatusLine().getStatusCode() == 200) {
        Gson gson = new GsonBuilder().create();
        // parse the response as T and and assign to object of ResponseModel 
        responseModel.object = ...
    }
    else
    {
        responseModel.message = response.getEntity().getContent();
    }
// return ResponseModel here
}

C# 中的泛型和 Java 非常不同。简单来说,你在这里做的事情没有任何意义。

您正在使用的 java 泛型 T 有一个 compile 时间特性。它允许您在 compile 时使用更具体的类型,而不是到处使用 Object

因此您不能使用泛型来确定 "runtime" 处的 "T",正如您可能想要的那样。您方法中的 T 来自 "outside",而 编译器 确定在其他情况下它应该是 ResponseModel<Integer>ResponseModel<Whatever>

您不能让 gson 为您读取 JSON 数据到 return 特定的 ResponseModel<Whatever>。如果有的话,您也许可以使用 TypeAdapter magic 根据实际值进行一些切换,到 return 这个或那个 specific ResponseModel<Foo> .

除此之外:当使用像 类 这样的 bean 作为您的 ResponseModel 时,您只希望它们是 特定的 ,不是通用的。

不确定,但我有类似的要求,但它适用于 Android。这是参考 link,我必须在其中编写一个通用的 class 以从 Assets 文件夹加载不同形式的 JSON 并解析为 POJO class.

https://github.com/gokulnathperiasamy/Android-Helper/blob/master/JSONHelper.java

代码:

    private static String convertJSONtoEntity(String jsonString, String typeString) {
        String jsonObjectString = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            jsonObjectString = jsonObject.get(typeString).toString();
        } catch (Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return jsonObjectString;
    }

    private static <T> List<T> fromJsonList(String json, Class<T> clazz) {
        Object[] array = (Object[]) java.lang.reflect.Array.newInstance(clazz, 0);
        array = new Gson().fromJson(json, array.getClass());
        List<T> list = new ArrayList<>();
        if (array != null && array.length > 0) {
            for (Object anArray : array) {
                list.add(clazz.cast(anArray));
            }
        }
        return list;
    }

用法:

用您的 jsonString 调用 convertJSONtoEntity()typeString 将成为您的 JSON.

的根元素

使用 convertJSONtoEntity()Class 返回的值调用 fromJsonList()。这给出了来自 JSON.

的对象列表

借助其他答案并在 Google 中进一步搜索,我最终得到以下代码

public static <T> ResponseModel<T> Get(Class<?> classType, String requestUri) throws ClientProtocolException, IOException 
{
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(requestUri);
    httpGet.addHeader("accept", "application/json");
    HttpResponse response = client.execute(httpGet);
    ResponseModel<T> responseModel = new ResponseModel<T>();
    if (response.getStatusLine().getStatusCode() == 200) 
    {
        responseModel.setObject((T) Utils.fromJson(EntityUtils.toString(response.getEntity()), classType));
        responseModel.setIsRequestSuccessful(true);
    } 
    else 
    {
        responseModel.setMessage(response.getEntity().getContent().toString());
        responseModel.setIsRequestSuccessful(false);
    }
    return responseModel;
}