使用GSON序列化时,出现“声明多个JSON字段已命名”错误

时间:2018-10-20 07:33:41

标签: java android json android-studio gson

Type type = new TypeToken<HashMap<String , MovieDb>>() {}.getType();
gson.fromJson(json, type); //ERROR HERE !!!

当我从Json转换为hashmap对象时,它可以正常工作而无需启用缩小功能。但是启用minify时,在此行出现以下错误:

java.lang.IllegalArgumentException: class a.a.a.a.b declares multiple JSON fields named a
        at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:172)
        at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:102)
        at com.google.b.e.a(Gson.java:458)
        at com.google.b.b.a.b.a(CollectionTypeAdapterFactory.java:53)
        at com.google.b.e.a(Gson.java:458)
        at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:117)
        at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:166)
        at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:102)
        at com.google.b.e.a(Gson.java:458)
        at com.google.b.b.a.g.a(MapTypeAdapterFactory.java:126)
        at com.google.b.e.a(Gson.java:458)
        at com.google.b.e.a(Gson.java:926)
        at com.google.b.e.a(Gson.java:892)
        at com.google.b.e.a(Gson.java:841)
        at com.techy.nateshmbhat.moviego.i.onPreExecute(MovieInterface.java:180)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:648)
        at android.os.AsyncTask.execute(AsyncTask.java:595)
        at com.techy.nateshmbhat.moviego.i.b(MovieInterface.java:101)
        at com.techy.nateshmbhat.moviego.a.a(Activity_InTheaterMovies.java:55)

1 个答案:

答案 0 :(得分:2)

您的错误最可能是因为您将两个字段缩小为相同的名称a。像这样:

@SerializedName("a")
Long veryLongFieldName;

@SerializedName("a")
Long anotherVeryLongFieldName;

请注意,它们可以在同一类中,或者如果使用继承,则它们在同一继承树中就足够了。 Gson无法将两个值分配给一个最小化的字段名称a

当然也可能是您已将某些字段最小化为一个名称,而该名称已被其他未最小化的字段保留。