无法使用 GSON 将 class 转换为 JSON
Unable to convert class to JSON using GSON
当我尝试将 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));
Comanda class 看起来像这样:
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 中的任何解决方案
我认为问题出在 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;
}
}
当我尝试将 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));
Comanda class 看起来像这样:
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 中的任何解决方案
我认为问题出在 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;
}
}