java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to xxxxxx

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to xxxxxx


The reason is that when converting ordinary String strings to List objects, the solution can be the following conversion methods:

    /**
     * Convert a json string to a List containing POJO objects
     *
     * @param string
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonStringConvertToList(String string, Class<T[]> cls) {
        Gson gson = new Gson ();
        T[] array = gson.fromJson(string, cls);
        return Arrays.asList(array);
    }

For example, a Java object MyPOJO.class of your own, then the method is:

jsonStringConvertToList(string,MyPOJO[].class);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447066&siteId=291194637