我有一个看起来像这样的 json
{
user:{
name:abc
email:a@bc.com
.....
}
responseCode:1
responseMessage:Done
}
我正在使用带有 GSON 转换器的 Retrofit 2。2 个参数 responseCode和responseMessage出现在每个 JSON 中。区分参数是“用户”
所以我创建了一个类
class BaseResponse {
public int responseCode;
public String responseMessage;
}
然后 POJOS 的其余部分扩展如下:
class User extends BaseResponse {
public String name;
public String email;
}
问题是,GSON 不会反序列化“用户”部分。我想我必须写一个反序列化器。
return new JsonDeserializer<T>() {
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
int messageCode = jsonObject.get("responseCode").getAsInt();
if (messageCode == 1) {
**// What do I do now?**
}
return null;
}
};
问题是,GSON 向我返回了用户对象元素全部为空的用户对象。
我有点在修复中。无论如何这是可能的吗?要么我通过做JsonObject jo = json.getAsJsonObject().getAsJsonObject("user");
或父母得到子类的东西。任何帮助,将不胜感激。