1 回答
TA贡献1802条经验 获得超10个赞
您可以使用 custom 来实现这一点JsonDeserializer。使用自定义反序列化器,您可以决定如何反序列化此类Key。在某处实现它,下面的内联示例:
public JsonDeserializer<Key> keyDs = new JsonDeserializer<Key>() {
private final Gson gson = new Gson();
@Override
public Key deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
// This will be valid JSON
String keyJson = json.getAsString();
// use another Gson to parse it,
// otherwise you will have infinite recursion
Key key = gson.fromJson(keyJson, Key.class);
return key;
}
};
注册GsonBuilder,创建Gson和反序列化:
Data[] mapPojos = new GsonBuilder().registerTypeAdapter(Key.class, ds).create()
.fromJson(x, Data[].class);
添加回答
举报