【发布时间】: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