() {}.getType(); gson.fromJson(json, type); //ERROR HERE !!! 当我从 Json 转换为 -6ren">
gpt4 book ai didi

java - 使用 GSON 序列化时出现 "declares multiple JSON fields named"错误

转载 作者:太空狗 更新时间:2023-10-29 13:03:01 26 4
gpt4 key购买 nike

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)

最佳答案

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

@SerializedName("a")
Long veryLongFieldName;

@SerializedName("a")
Long anotherVeryLongFieldName;

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

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

关于java - 使用 GSON 序列化时出现 "declares multiple JSON fields named"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52903443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com