你似乎有一些問題,你的JSON映射:你想綁定的超類字段子類字段,然而這是哪裏的接口可能是你更好的選擇,因爲你的意圖只是詢問反序列化的對象的id和名稱。
我會做這樣的:
interface LookUp {
int getId();
String getName();
}
final class CountryByInterface
implements LookUp {
@SerializedName("ID")
private final Integer id = null;
@SerializedName("CountryNameEN")
private final String name = null;
@Override
public int getId() {
return id;
}
@Override
public String getName() {
return name;
}
}
所以它可以很容易使用(Java的8只爲演示目的):
final Gson gson = new Gson();
final Type countryListType = new TypeToken<List<CountryByInterface>>() {
}.getType();
try (final Reader reader = getPackageResourceReader(Q43247712.class, "countries.json")) {
gson.<List<CountryByInterface>>fromJson(reader, countryListType)
.stream()
.map(c -> c.getId() + "=>" + c.getName())
.forEach(System.out::println);
}
如果出於某種正當理由你確實需要超級類來持有這樣的領域,你必須實現後處理器(靈感來自PostConstructAdapterFactory)。再說了,
abstract class AbstractLookUp {
int id;
String name;
abstract int getId();
abstract String getName();
final void postSetUp() {
id = getId();
name = getName();
}
}
final class CountryByClass
extends AbstractLookUp {
@SerializedName("ID")
private final Integer id = null;
@SerializedName("CountryNameEN")
private final String name = null;
@Override
int getId() {
return id;
}
@Override
String getName() {
return name;
}
}
final Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new TypeAdapterFactory() {
@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
// Check if it's a class we can handle: AbstractLookUp
if (AbstractLookUp.class.isAssignableFrom(typeToken.getRawType())) {
// Get the downstream parser for the given type
final TypeAdapter<T> delegateTypeAdapter = gson.getDelegateAdapter(this, typeToken);
return new TypeAdapter<T>() {
@Override
public void write(final JsonWriter out, final T value)
throws IOException {
delegateTypeAdapter.write(out, value);
}
@Override
public T read(final JsonReader in)
throws IOException {
// Deserialize it as an AbstractLookUp instance
final AbstractLookUp abstractLookUp = (AbstractLookUp) delegateTypeAdapter.read(in);
// And set it up
abstractLookUp.postSetUp();
@SuppressWarnings("unchecked")
final T result = (T) abstractLookUp;
return result;
}
};
}
return null;
}
})
.create();
final Type countryListType = new TypeToken<List<CountryByClass>>() {
}.getType();
try (final Reader reader = getPackageResourceReader(Q43247712.class, "countries.json")) {
gson.<List<CountryByClass>>fromJson(reader, countryListType)
.stream()
.map(c -> ((AbstractLookUp) c).id + "=>" + ((AbstractLookUp) c).name)
.forEach(System.out::println);
}
兩個例子都產生
5 = >英國
6 = >美國
但是我覺得第一種方法更好地設計和多更容易使用,哪裏第二個演示如何配置Gson來實現複雜的(de)序列化策略。
Lookup ID和Country iD是不同的嗎? –
不,相同的值 –
那你爲什麼需要超類? –