如何处理“无法转换为类"?错误-GSON JAVA [英] How to deal with "cannot be cast to class" error - GSON JAVA

查看:155
本文介绍了如何处理“无法转换为类"?错误-GSON JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的列表写入抽象模型中的jtable,然后将其返回给我这个错误.我认为可能是列表格式引起的?名称和金额放在错误的位置.这是我的完整错误消息: 线程"AWT-EventQueue-0"中的异常java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap类无法转换为Model.Medicine类(com.google.gson.internal.LinkedTreeMap和Model.Medicine位于加载程序"app"的未命名模块)

i am trying to write my list to jtable in abstract model and then its return to me this error. In my opinion it could be cause by list format? name and amount are in wrong place. This is my full error message: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class Model.Medicine (com.google.gson.internal.LinkedTreeMap and Model.Medicine are in unnamed module of loader 'app')

有一个代码: 这是我的医学课

There is a code: This is mine Medicine class

public class Medicine {

private String name;
private String amount;

public Medicine( String amount, String name){
    this.name = name;
    this.amount = amount;
}

public String getName() {return name;}
public void setName(String name){this.name = name;}

public String getAmount(){return amount;}
public void setAmount(String amount){this.amount = amount;}
}

这是我的转换代码:

public List<Medicine> FromJsonToArray() throws IOException {
    String medicineJson = initArray("Medicines.json").toString();
    Gson gson = new Gson();
    List<Medicine> medicineArray = gson.fromJson(medicineJson, List.class);
    return medicineArray;
    }

转换后,我的列表如下:

After convert my List looks like:

[{amount=123,name=Ibuprofen},{amount=333,name=Ketonal},...]

我的json:

[
{
    "amount": "123",
    "name": "Ibuprofen"
},
{
    "amount": "333",
    "name": "Ketonal"
}
]

最后有错误的表模型类:

And finally table model class with error:

public class MedicineTableModel extends AbstractTableModel {
private List<Medicine> medicines;
private String[] columns;

public  MedicineTableModel(List<Medicine> aMedicineList){
    super();
    medicines = aMedicineList;
    columns = new String[]{"Name", "Amount"};
}


@Override
public int getRowCount() {
    return medicines.size();
}

@Override
public int getColumnCount() {

    return columns.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex)
{
    if (columnIndex<medicines.size())
    {
        Medicine c= medicines.get(rowIndex); <---- there is a problem :O
        if(rowIndex == 0) {

            return(c.getName());
        }
     }
    return null;
}
public String getColumnName(int col) {
    return columns[col] ;
}
}

推荐答案

public List<Medicine> FromJsonToArray() throws IOException {
    String medicineJson = initArray("Medicines.json").toString();
    Gson gson = new Gson();
    Type medicineListType = new TypeToken<List<Medicine>>() {}.getType();
    List<Medicine> medicineArray = gson.fromJson(medicineJson, medicineListType);
    return medicineArray;
}

应该可以解决问题.

基本上,告诉gson将其反序列化为特定类的列表是一种解决方法.

Basically it is a workaround to tell gson to deserialize to a list of particular class.

这是必需的,因为泛型在运行时会丢失并且您的json被反序列化为List<LinkedTreeMap>而不是List<Medicine>.

That is required because generics are lost at runtime and your json was being deserialized to List<LinkedTreeMap> instead of List<Medicine>.

这篇关于如何处理“无法转换为类"?错误-GSON JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆