java - 你为什么要把那些无效的 child 还给我?

标签 java gson

我正在使用 GSON 2.6.1 库(适用于 Java 1.6,因为它是一个旧应用程序)。

我正在从 API 检索数据,但想要使用 GSON 重新生成对象,它返回带有空对象的属性之一(列表)。

我做错了什么?

我的代码:

public class ListarResponse implements Serializable{

    private static final long serialVersionUID = -1451345915213315934L;
    private String codigo;
    private String descripcion;
    private List<Propiedad> documentos;

    public ListarResponse(){
    };

    public ListarResponse(String codigo, String descripcion, List<Propiedad> documentos) {
        super();
        this.codigo = codigo;
        this.descripcion = descripcion;
        this.documentos = documentos;
    }

    public String getCodigo() {
        return codigo;
    }
    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    public List<Propiedad> getDocumentos() {
        return documentos;
    }
    public void setDocumentos(List<Propiedad> documentos) {
        this.documentos = documentos;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}

    public ListarResponse listarDocumentosFilenet(ListarRequest request,URL endpoint,String method) {
        ListarResponse response = null;
        try {
            url = endpoint;
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod(method);
            Gson g = new Gson();
            String requestJSON = g.toJson(request);
            OutputStream os = conn.getOutputStream();
            os.write(requestJSON.getBytes("UTF-8"));
            os.close();
            if (conn.getResponseCode() != 200) {
                log.error("Ocurrio un error con la peticion HTTP, terminó con codigo: " + conn.getResponseCode());
                throw new RuntimeException("Ocurrio un error con la peticion HTTP, terminó con codigo: "
                        + conn.getResponseCode());
            }else{
                InputStream in = new BufferedInputStream(conn.getInputStream());
                String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
                response = g.fromJson(result, ListarResponse.class);
                System.out.println(result);
                in.close();
            }
            conn.disconnect();
        } catch (Exception e) {
            conn.disconnect();
            System.out.println("Exception in NetClientGet:- " + e);
        }
        return null;
    }

在这一部分中,response = g.fromJson(result, ListarResponse.class);,是这样的:

Image

Json 响应:

{
    "codigo": 0,
    "descripcion": "Documentos listados correctamente.",
    "documentos": [
        {
            "propiedades": [
                {
                    "nombre": "DateCreated",
                    "valor": "2020-02-11T04:58:46.980"
                },
                {
                    "nombre": "DocumentTitle",
                    "valor": "PreacuerdoPrestamoHipotecario"
                },
                {
                    "nombre": "Id",
                    "valor": "{05F00622-498A-423B-99D0-A210C2DE11FA}"
                },
                {
                    "nombre": "OD_FechaEmision",
                    "valor": "2018-04-13T12:00:00.000"
                },
                {
                    "nombre": "OD_IdTipoDoc",
                    "valor": "1323"
                },
                {
                    "nombre": "OD_LegajoAlta",
                    "valor": "L0647764"
                },
                {
                    "nombre": "OD_LegajoDigitalizacion",
                    "valor": "L0647764"
                },
                {
                    "nombre": "OD_NombreImagen",
                    "valor": "PreacuerdoPrestamoHipotecario"
                },
                {
                    "nombre": "POD_IdHostMulti",
                    "valor": "1234"
                },
                {
                    "nombre": "POD_ProcesoMulti",
                    "valor": "0011|02|1-9IU4OVD"
                }
            ]
        },
        {
            "propiedades": [
                {
                    "nombre": "DateCreated",
                    "valor": "2020-01-17T02:12:57.107"
                },
                {
                    "nombre": "DocumentTitle",
                    "valor": "PreacuerdoPrestamoHipotecario"
                },
                {
                    "nombre": "Id",
                    "valor": "{74CA29A2-3AC7-45F4-BF6F-132D44258F27}"
                },
                {
                    "nombre": "OD_FechaEmision",
                    "valor": "2018-04-13T12:00:00.000"
                },
                {
                    "nombre": "OD_IdTipoDoc",
                    "valor": "1323"
                },
                {
                    "nombre": "OD_LegajoAlta",
                    "valor": "L0647764"
                },
                {
                    "nombre": "OD_LegajoDigitalizacion",
                    "valor": "L0647764"
                },
                {
                    "nombre": "OD_NombreImagen",
                    "valor": "PreacuerdoPrestamoHipotecario"
                },
                {
                    "nombre": "POD_IdHostMulti",
                    "valor": "1234"
                },
                {
                    "nombre": "POD_ProcesoMulti",
                    "valor": "0011|02|1-9IU4OVD"
                }
            ]
        }
    ]
}

我添加了一个图像,以防 JSON 不可见。

Image2

最佳答案

documentos字段不是 Propiedad 的列表对象,其中 Propiedad是一个带有字段 nombre 的类和valor .

documentosList<Foo> (该示例 JSON 中有 2 个 Foo 对象)和 Foo 1 有一个名为 propiedades 的字段这是 List<Propiedad> .

1) Foo当然是一个占位符名称供您决定。

关于java - 你为什么要把那些无效的 child 还给我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60891531/

相关文章:

java - Joda - 获得夏令时独立时间的正确方法

java - 将字符串值转换为类型

java - Selenium RC Java - isElementPresent 不起作用

javascript - 在 javascript 模式下将连接器类型从 javascript 更改为数据库读取器?

java - 由于构造函数中的静态类,Mockito 无法使用 @InjectMocks 创建实例

android - Retrofit 将 json 变量映射到关键字

json - 如何在 Node.js 中解析包含 "NaN"的 JSON 字符串

java - future 完成后返回 Ajax 响应

java - Gson 预期为 BEGIN_ARRAY,但对于类型 Byte[] 却是 BEGIN_OBJECT

java - 使用gson读取java中的json对象文件不断获取null