Gson序列化为LinkedTreeMap而不是我的实体

时间:2014-03-30 04:25:33

标签: java android rest serialization gson

我正在尝试在Android应用程序中使用REST服务,但在序列化ArrayList时出现问题:

我的RESTful服务返回:

enter image description here

我的Android应用程序收到:

enter image description here

我正在使用Gson库来序列化我的对象,当然问题与它有关。在这个意义上,我尝试按如下方式制作Serializer:

public class MySerializer<E> 
    implements JsonSerializer<Collection<E>>, JsonDeserializer<Collection<E>> {

    @Override
    public Collection<E> deserialize(JsonElement json, Type typeOfT, 
        JsonDeserializationContext context) throws JsonParseException {
        Type type = new TypeToken<List<E>>(){}.getType();
        return context.deserialize(json, type);
    }
    @Override
    public JsonElement serialize(Collection<E> src, Type typeOfSrc, 
        JsonSerializationContext context) {
        Type type = new TypeToken<List<E>>(){}.getType();
        return context.serialize(src, type);
    }
}

注册了apapter(registerTypeAdapter(ArrayList.class, new MySerializer<Object>())),但这不起作用......

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

是否可以尝试将gradle版本更改为更新版本,并检查问题是否已解决。

我也有同样的问题,当我将gradle版本从1.5.0更改为2.1.0时它得到了解决