【问题标题】:Gson.toJson()"Exception java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits"Gson.toJson()"异常 java.lang.IllegalArgumentException: 类 java.text.DecimalFormat 声明了多个名为 maximumIntegerDigits 的 JSON 字段"
【发布时间】:2019-07-30 11:58:37
【问题描述】:

我正在尝试制作一个 JSON 文件以将其用作数据库。我的课程是: 1.绵羊:包括关于每只羊的一些信息 2. 农场:包括一系列绵羊 3. DB:这里我正在尝试将 Farm 对象解析为 JSON

我已经尝试将“瞬态”添加到日期类型的所有字段中,但没有成功。

为了让您更好地了解我想要做什么:它是一个农场经理,用户可以在其中存储有关他的羊的数据

public class Sheep implements Serializable {

    @SerializedName("localId")
    @Expose
    private int localId;
    @SerializedName("globalId")
    @Expose
    private int globalId;
    @SerializedName("birthDate")
    @Expose
    private transient Date birthDate = null;
    @SerializedName("sellingDate")
    @Expose
    private transient Date sellingDate = null;
    @SerializedName("deathDate")
    @Expose
    private transient Date deathDate = null;
    @SerializedName("lastpregnancyDate")
    @Expose
    private transient Date lastpregnancyDate = null;
    @SerializedName("lastDoctorCheck")
    @Expose
    private transient Date lastDoctorCheck = null;
    @SerializedName("alive")
    @Expose
    private boolean alive;
    @SerializedName("sold")
    @Expose
    private boolean sold;
    @SerializedName("pregnant")
    @Expose
    private boolean pregnant;
    @SerializedName("mother")
    @Expose
    private Sheep mother = null;
    @SerializedName("father")
    @Expose
    private Sheep father = null;
    @SerializedName("allDoctorChecks")
    @Expose
    private transient List<Date> allDoctorChecks = null;
    @SerializedName("allpregnancies")
    @Expose
    private transient List<Date> allpregnancies = null;
    @SerializedName("children")
    @Expose
    private List<Sheep> children = null;
    @SerializedName("allHusbands")
    @Expose
    private List<Sheep> allHusbands = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
}
public class Farm implements Serializable {

    @SerializedName("allSheeps")
    @Expose
    private List<Sheep> allSheeps = null;
}
public class DataBase {
    private static Farm farm;

public static void main(String[] args){
        ...

        Gson g = new GsonBuilder().setPrettyPrinting().create();
**/*40*/  String strGsn = g.toJson(farm);/*this is line 40*/** 

        FileWriter writer = null;
        try{
            writer = new FileWriter("farm.json");
            writer.write(strGsn);
        }catch (Exception e){
            System.out.println(e.fillInStackTrace());
        }finally {
            if(writer != null)
                try{
                    writer.close();
                }catch (Exception e){
                    System.out.println(e.fillInStackTrace());
                }
        }


    }

}
"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\lib\idea_rt.jar=53834:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\saleh\Desktop\farm\out\production\farm;C:\Users\saleh\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar com.company.DB.DataBase
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.gson.internal.reflect.UnsafeReflectionAccessor (file:/C:/Users/saleh/.m2/repository/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar) to field java.text.SimpleDateFormat.serialVersionOnStream
WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.reflect.UnsafeReflectionAccessor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.Gson.toJson(Gson.java:704)
    at com.google.gson.Gson.toJson(Gson.java:683)
    at com.google.gson.Gson.toJson(Gson.java:638)
    at com.google.gson.Gson.toJson(Gson.java:618)
    at com.company.DB.DataBase.main(DataBase.java:40)

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    异常

    java.lang.IllegalArgumentException:类 java.text.DecimalFormat 声明了多个名为 maximumIntegerDigits 的 JSON 字段

    是由于Sheep 的字段sdf 正在序列化。为什么SimpleDateFormat不能序列化成json是有问题的,但是为了解决这个问题,既然你是用@Expose注解来控制要序列化哪个字段,改行
    Gson g = new GsonBuilder().setPrettyPrinting().create();

    Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
    为了避免sdf被序列化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2019-08-23
      • 2021-02-13
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多