GSON Generic:无法反序列化json对象

时间:2014-01-14 18:03:15

标签: android json rest deserialization gson

我正在编写休息服务。我尝试回复的对象是通用的:

public class ResultadoBaseJsonForm<ID extends Serializable,E> implements Serializable{

private String resultado;
private String mensaje;
private List<ObjectError> errores;
private Map<String,String> erroresValidacion;
private ID id;
private E obtjetoTransporte;

实施的对象是:

ResultadoBaseJsonForm<Long,List<Reference>>

在客户端,代码为:

Gson gson = new Gson();
Type collectionType = new TypeToken<ResultadoBaseJsonForm<Long, List<Reference>>>(){}.getType();
ResultadoBaseJsonForm<Long, List<Reference>> objectResponse =   gson.fromJson(jsonresult, collectionType);

消息是:

{"resultado":"OK","mensaje":"Referencias consultadas correctamente","errores":null,"erroresValidacion":null,"id":null,"obtjetoTransporte":[{"code":"140.12","type":"1","family":"1","tittle":"1","descripcion":"1","drawable":"","drawableSmall":""},"code":"140.6","type":"1","family":"1","tittle":"1","descripcion":"1","drawable":"","drawableSmall":""}]}

我收到了这个错误:

The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@a7415fb0 failed to deserialized json object 
[{"code":"140.12","type":"1","family":"1","tittle":"1","descripcion":"1","drawable":"","drawableSmall":""},{"code":"140.6","type":"1","family":"1","tittle":"1","descripcion":"1","drawable":"","drawableSmall":""}] given the type java.util.List<com.six.tma.bean.Reference>

参考类是:

public class Reference implements Serializable, Cloneable {
@DatabaseField(id = true) 
private String code;

@DatabaseField(canBeNull=false)
private String family;
@DatabaseField
private String tittle;
@DatabaseField(canBeNull=false)
private String type;
@DatabaseField
private String descripcion;
@DatabaseField
private String promotion;
@DatabaseField
private String mechanic;
@DatabaseField
private String period;
@DatabaseField
private String quota;

@DatabaseField(dataType = DataType.BYTE_ARRAY)
private byte[] drawable;
@DatabaseField(dataType = DataType.BYTE_ARRAY)
private byte[] drawableSmall;

任何帮助?


抱歉,我附上了错误的JSON:

{"resultado":"OK","mensaje":"Referencias consultadas correctamente","errores":null,"erroresValidacion":null,"id":null,"obtjetoTransporte":[{"code":"140.12","type":"1","family":"1","tittle":"1","descripcion":"1","drawable":"","drawableSmall":""},{"code":"140.6","type":"1","family":"1","tittle":"1","descripcion":"1","drawable":"","drawableSmall":""}]}

我在jsonlint.org点击测试了它,它是有效的。我在春天做了它。

任何帮助?

1 个答案:

答案 0 :(得分:2)

您的JSON错误,我不知道您是如何在服务器端生成的,但在第二个“代码”令牌之前缺少[

如果你有一个用Java编码的服务器,你也可以使用Gson序列化你的响应。

正如评论中所建议的那样,您可以利用http://json.parser.online.fr/等服务检查JSON,以便在解析错误时检查您的JSON。