在检索这些自定义对象时,使用gson库将自定义对象的ArrayList转换为JSON,ClassCastException

时间:2016-06-18 13:19:12

标签: android json gson classcastexception

我已使用Google的GSON库将ArrayList我的自定义对象转换为JSON,以便将其存储到SharedPreferences。存储在ArrayList中的对象类是这样的:

class CustomObject {
  String name;
  Map<String, Long>  topGrades;
  Map<String, Long> lowestGrades;

  CustomObject(String name, Map<String, Long>  topGrades, Map<String, Long>  lowestGrades) {
    this.name = name;
    this.lowestGrades = lowestGrades;
    this.topGrades = topGrades;
  }
}

我将ArrayList CustomObjectSharedPreferences对象保存到List<CustomObject> dataList = new ArrayList<>(); //Populate the list with objects of type CustomObject ... SharedPreferences sharedPreferences = getActivity().getSharedPreferences( "tests.dataSharedPreferences", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); Gson gson = new Gson(); String dataListJson = gson.toJson(dataList); editor.putString("tests.dataListJsonKey", dataListJson); editor.commit(); ,如:

List

我从JSON中检索了SharedPreferences sharedPreferences = getActivity().getSharedPreferences("tests.dataSharedPreferences", Context.MODE_PRIVATE); String dataJson = sharedPreferences.getString("tests.dataListJsonKey", ""); Gson gson = new Gson(); final List<CustomObject> dataList = gson.fromJson(dataJson, ArrayList.class); ,如:

name

非常好。但是现在为了将上面CustomObject中所有dataList的{​​{1}} s变成一个String数组,在下面的循环中,我试图从CustomObject中检索dataList ClassCastException但我得到 String[] namesArray = new String[dataList.size()]; for (int x=0; x<namesArray.length; x++) { CustomObject customObject = dataList.get(x);//********ClassCastException******************************** namesArray[x] = customObject.name; }

06-18 12:28:11.689: E/AndroidRuntime(1536): Caused by: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to tests.CustomObject

library(ggplot2)
ggplot(data =  dframe, mapping = aes(x = label, y = method)) +
  geom_tile(aes(fill = value), colour = "white") +
  geom_text(aes(label = sprintf("%1.0f",value)), vjust = 1) +
  scale_fill_gradient(low = "white", high = "steelblue")

问题是为什么以及解决方案是什么?

2 个答案:

答案 0 :(得分:2)

只需替换

 final List<CustomObject> dataList = gson.fromJson(dataJson, ArrayList.class);

final List<CustomObject> dataList = gson.fromJson(data, new TypeToken<List<CustomObject>>() {
          }.getType());

答案 1 :(得分:1)

Gson必须知道要转换为的对象类型。也就是说,为了序列化,它必须知道确切的类型。当您将类型作为ArrayList.class传递时,它不会提供完整的信息。

您可以做的一件事是创建一个包含您的arraylist的包装类。然后将该类作为第二个参数提供。

class Wrapper{
    ArrayList<CustomObject> dataList; 
    //constrctor 
    public Wrapper(){
       //empty constructor  for Gson
    }
    //setter for dataList
    public setDataList(ArrayList<CustomObject> dataList){
        this.dataList = dataList;
    }
}

//尝试将其用于序列化和反序列化

Wrapper wrapper = new Wrapper();
//populate list
wrapper.setDataList(list);

//serialize
String dataListJson = gson.toJson(wrapper);

//deserialize
Gson gson = new Gson();
Wrapper dataWrapper = gson.fromJson(dataListJson, Wrapper.class);
}