RuntimeTypeAdapterFactory表示“类型”未定义 [英] RuntimeTypeAdapterFactory says “type” not defined
问题描述
Class Pen {
$ p $ >
字符串名称;
列表< Animal>动物;
}
// Animal可以是一个接口或父类:我很灵活
Class Animal {
AnimalType类型; //枚举
int腿;
}
enum AnimalType {
狗,猫,猪,鸡;
}
类AnimalDog扩展动物{
// ...
}
类AnimalCat extends Animal {
// ...
}
类AnimalPig extends Animal {
// ...
}
然后我创建我的Gson实例,其中包含
pre $ public static Gson instanceUpperCamelCaseWithTypeAdapterFactory() {
if(null == sGsonUpperCamelCase){
final RuntimeTypeAdapterFactory< Animal> typeFactory = RuntimeTypeAdapterFactory
.of(Animal.class,type)
.registerSubtype(AnimalDog.class,dog)
.registerSubtype(AnimalCat.class,cat)
.registerSubtype(AnimalPig.class,pig);
sGsonUpperCamelCase = new GsonBuilder()。setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.registerTypeAdapterFactory(typeFactory).create();
} //命名策略是因为服务器向我发送大写字段而Java字段是小写字母
return sGsonUpperCamelCase;
}
为了从包含动物列表的json获取动物,我做了
List< Animal>动物= gson.fromJson(json,new TypeToken< List< Animal>>(){} .getType());
我完全是Gson的新手,所以没有太多的困惑,我该如何解决他的问题?
$ b
错误追踪:
com.google.gson .JsonParseException:无法反序列化com.company.appname.data.model.Animal类,因为它没有定义名为type
的字段com.company.appname.utils.RuntimeTypeAdapterFactory $ 1.read(RuntimeTypeAdapterFactory.java:204)
com.google.gson.TypeAdapter $ 1.read(TypeAdapter.java:199)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
com。 google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:82)
com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:61)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:117)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:217)
com.google.gson.internal.bind.TypeAdapterRuntimeType Wrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:82)
com.google.gson.internal.bind。 CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:61)
com.google.gson.Gson.fromJson(Gson.java:861)
com.google.gson.Gson.fromJson(Gson.java: 826)
com.google.gson.Gson.fromJson(Gson.java:775)
我通过在线验证器运行json,没有问题。它有一些项目。
{Animals:[{
id:9,
type:dog,
name:maximus
},
{
id:10,
type:cat ,
name:meowy,
yarns:5,
nice:true
}]}
解决方案RuntimeTypeAdapterFactory.class
行号 - > 203
需要替换
JsonElement labelJsonElement = jsonElement.getAsJsonObject()。remove typeFieldName);
with
JsonElement labelJsonElement = jsonElement.getAsJsonObject()。get(typeFieldName);
I am dealing with a situation where I have polymorphic classes that I need to deserialize.
Class Pen{ String name; List<Animal> animals; } //Animal can be an interface or parent class: I am flexible Class Animal{ AnimalType type;//enum int legs; } enum AnimalType{ dog,cat,pig,chicken; } Class AnimalDog extends Animal{ //… } Class AnimalCat extends Animal{ //… } Class AnimalPig extends Animal{ //… }
then I create my Gson instance with
public static Gson instanceUpperCamelCaseWithTypeAdapterFactory() { if (null == sGsonUpperCamelCase) { final RuntimeTypeAdapterFactory<Animal> typeFactory = RuntimeTypeAdapterFactory .of(Animal.class, "type") .registerSubtype(AnimalDog.class, "dog") .registerSubtype(AnimalCat.class, "cat") .registerSubtype(AnimalPig.class, "pig"); sGsonUpperCamelCase = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .registerTypeAdapterFactory(typeFactory).create(); }//the naming policy is because server sends me upper case fields whereas Java fields are lowercase. return sGsonUpperCamelCase; }
To get the animals from a json that contains a list of animals, I do
List<Animal> animals = gson.fromJson(json, new TypeToken<List<Animal>>() {}.getType());
I am completely a newbie to Gson. Completely. So without confusing me too much, how might I solve this problem?
Error trace:
com.google.gson.JsonParseException: cannot deserialize class com.company.appname.data.model.Animal because it does not define a field named type com.company.appname.utils.RuntimeTypeAdapterFactory$1.read(RuntimeTypeAdapterFactory.java:204) com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199) com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117) com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217) com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) com.google.gson.Gson.fromJson(Gson.java:861) com.google.gson.Gson.fromJson(Gson.java:826) com.google.gson.Gson.fromJson(Gson.java:775)
I run the json through an online validator, there is no problem with it. It has a number of items. Here I show two.
{"Animals":[{ "id":9, "type":"dog", "name":"maximus" }, { "id":10, "type":"cat", "name":"meowy", "yarns":5, "nice":true }]}
解决方案RuntimeTypeAdapterFactory.class line number -> 203
need replace
JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
with
JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
这篇关于RuntimeTypeAdapterFactory表示“类型”未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!