无法使用GSON将类转换为JSON

wfypjpf4  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(263)

当我尝试将我的Android应用程序中的对象转换为Json时,我得到以下错误:

class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations

问题在于,在某些设备中,所有设备都是工作GSON版本:

implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

虽然我试图转换的对象的代码如下:

Comanda comanda = new Comanda(
                    tasto.getDescriptionReceipt(),
                    quantitaSelezionata,
                    tasto.getCodice(),
                    turnoValue,
                    currentTimeString,
                    operatore,
                    "A",
                    String.valueOf(tasto.getPrice()));
            intent.putExtra("prodotto", new Gson().toJson(comanda));

科曼达类看起来像这样:

public class Comanda {

    private final String descProdotto;
    private double qta;
    private int turno;
    private String codice;
    private String time;
    private String codOP;
    private String state;
    private String pre;
    private ArrayList<Varianti> variants = new ArrayList<>();

    public Comanda(String button, double qta, String codice, int turno, String time, String codOP, String state, String pre) {
        this.descProdotto = button;
        this.qta = qta;
        this.codice = codice;
        this.turno = turno;
        this.time = time;
        this.codOP = codOP;
        this.state = state;
        this.pre = pre;
    }

}

public class Varianti implements Serializable {

    String name;
    private String codice;
    BitmapDrawable drawable;
    private String state;
    public String pre;
    public String qta;

    public Varianti(String name, String codice, BitmapDrawable drawable, String state, String pre, String qta) {
        this.name = name;
        this.codice = codice;
        this.drawable = drawable;
        this.state = state;
        this.pre = pre;
        this.qta = qta;
    }
}

任何解决方案,即使在Kotlin也是受欢迎的。

8hhllhi2

8hhllhi21#

我认为问题出在Varianti bean上,它有一个属性BitmapDrawable。当你用Gson把它转换成字符串时,你不应该处理它。

ublic class Varianti implements Serializable {

    String name;
    private String codice;
    private transient BitmapDrawable drawable;
    private String state;
    public String pre;
    public String qta;

    public Varianti(String name, String codice, BitmapDrawable drawable, String state, String pre, String qta) {
        this.name = name;
        this.codice = codice;
        this.drawable = drawable;
        this.state = state;
        this.pre = pre;
        this.qta = qta;
    }
}

相关问题