java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 异常问题解决

时间:2024-04-09 08:12:38

问题

使用 Gson的 fromJson方法字符转成对象,操作转换的对象一直报错

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 异常问题解决

部分代码如下

public class TestDataPojo<T> extends BaseEntity {

    private T data;
    private TestData testData;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public TestData getTestData() {
        return testData;
    }

    public void setTestData(TestData testData) {
        this.testData= testData;
    }
}

public static <T> T jsonToObject(String jsonStr, Class<T> clazz) {
	return getGson().fromJson(jsonStr,clazz);
}


public static Gson getGson() {
	GsonBuilder gsonBuilder = new GsonBuilder();
	gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
	gsonBuilder.registerTypeAdapter(Timestamp.class,
				new TimestampTypeAdapter());
	gsonBuilder.disableHtmlEscaping();
	Gson gson = gsonBuilder.create();
	return gson;
}

public void getWareList() {
    try {
       List<TestDataPojo<TestPojo>> returnList = jsonToObject(testListStr,List.class);
       TestPojo testPojo = siteDataPojos.get(0).getData();
    } catch (Exception e) {
    }
    return null;
}

调试到  TestPojo testPojo = siteDataPojos.get(0).getData(); 代码就报错

问题原因:

GSON 解析的时候不知道TestDataPojo<T>中T具体是什么类型

解决方法:

使用GSON转换格式的时候指定具体类型就可以转换成功

/**
  *新添加根据具体类型转换对象方法
  **/
public static <T> T jsonToObject(String jsonStr, Type listType) {
        return getGson().fromJson(jsonStr,listType);
}

Type listType = new TypeToken<List<SiteDataPojo<WarePojo>>>() {}.getType();
List<TestDataPojo<TestPojo>> returnList = jsonToObject(testListStr,listType);

修改代码如下

public class TestDataPojo<T> extends BaseEntity {

    private T data;
    private TestData testData;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public TestData getTestData() {
        return testData;
    }

    public void setTestData(TestData testData) {
        this.testData= testData;
    }
}

public static <T> T jsonToObject(String jsonStr, Class<T> clazz) {
	return getGson().fromJson(jsonStr,clazz);
}

/**
  *新添加根据具体类型转换对象方法
  **/
public static <T> T jsonToObject(String jsonStr, Type listType) {
		return getGson().fromJson(jsonStr,listType);
}


public static Gson getGson() {
	GsonBuilder gsonBuilder = new GsonBuilder();
	gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
	gsonBuilder.registerTypeAdapter(Timestamp.class,
				new TimestampTypeAdapter());
	gsonBuilder.disableHtmlEscaping();
	Gson gson = gsonBuilder.create();
	return gson;
}

public void getWareList() {
    try {
       Type listType = new TypeToken<List<SiteDataPojo<WarePojo>>>() {}.getType();
       List<TestDataPojo<TestPojo>> returnList = jsonToObject(testListStr,listType);
       TestPojo testPojo = siteDataPojos.get(0).getData();
    } catch (Exception e) {
    }
    return null;
}