【问题标题】:GSON Generic: failed to deserialized json objectGSON Generic:无法反序列化 json 对象
【发布时间】:2023-03-20 19:28:02
【问题描述】:

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

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 click 上测试过,它是有效的。我在春天产生它。

有什么帮助吗?

【问题讨论】:

  • jsonlint.org 处插入您的代码点击验证,然后查看错误,您的 json 无效(您的数组中有名称-值对,而不是将它们包装在对象中)

标签: android json rest deserialization gson


【解决方案1】:

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

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

按照 cmets 中的建议,您可以利用 http://json.parser.online.fr/ 之类的服务检查您的 JSON,以便在发生解析错误时检查您的 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多