Deserialize JSON with dynamic field names

0

I have a Json like this, and inside the "buy" entity, I get an object with the id of the purchase, and then the information I need to access, I have already generated the classes, but my object that changes each name is getting null

{
   "Pedido" : 
   [
      {
         "compra" : {
            "107884" : {
               "produto" : [
                  {
                     "Codigo_barra" : "",
                     "Prod_nome" : "I",
                     "Sec_id" : "4364",
                     "Dep_id" : "1190",
                     "Preco_real" : "25.00",
                     "Qtd" : "1"
                  },
                  {
                     "Codigo_barra" : "1233211",
                     "Prod_nome" : "",
                     "Sec_id" : "4377",
                     "Dep_id" : "1193",
                     "Preco_real" : "91.00",
                     "Qtd" : "1"
                  }
               ],
               "Hora" : "1164242534",
               "Valor_entrada" : null,
               "Cielo" : null,
               "Valor_bonus" : null,
               "Bonus_desconto" : null,
               "DataFinal" : "2011-07-09 00:00:00",
               "cliente" : {
                  "PESnome" : "",
                  "PESddd1" : "018",
                  "PESnascdia" : "",
                  "PEStel1" : "",
                  "PEScpf" : "",
                  "PESemail" : ""
               },
               "Valor_desconto" : "0.00",
               "Numero_etiqueta" : null,
               "Lista_casamento" : null,
               "Loja_id" : "155",
               "Revendedor_id" : null,
               "Total" : null,
               "Valor_frete" : "11.40",
               "pagamento" : {
                  "CCCparcela" : "1",
                  "CCCtipo_cartao" : "",
                  "forma_de_pagamento" : "",
                  "CCCnum_cartao" : ""
               },
               "Mobile" : "0",
               "Parceiro" : "-",
               "Esitef" : null,
               "Compra_id" : "107884",
               "ENTtipofrete" : "N",
               "Status_atual" : "13",
               "Valor_tarifa" : "0.00",
               "enderecos" : {
                  "endereco_cobranca" : {
                     "PEScidade" : "",
                     "PEScep" : "19360000",
                     "PESuf" : "SP",
                     "PESbairro" : "CENTRO",
                     "PESlogradouro" : "",
                     "PESnumero" : "857"
                  },
                  "endereco_entrega" : {
                     "ENTbairro" : "",
                     "ENTuf" : "SP",
                     "ENTcidade" : "",
                     "ENTnumero" : "985",
                     "ENTlogradouro" : "",
                     "ENTcep" : "19360000",
                     "ENTtipo_end" : "Endereço"
                  }
               },
               "Data" : "2011-07-09",
               "Braspag" : null,
               "TpPessoa" : "F",
               "HoraGMT" : "Thu Nov 23 00:42:14 2006"
            }
         }
      },
      {
         "compra" : {
            "1652264" : {
               "produto" : [
                  {
                     "Codigo_barra" : "",
                     "Prod_nome" : "",
                     "Sec_id" : "4375",
                     "Dep_id" : "1192",
                     "Preco_real" : "14.39",
                     "Qtd" : "1"
                  }
               ],
               "Hora" : "1273684863",
               "Valor_entrada" : null,
               "Cielo" : null,
               "Valor_bonus" : null,
               "Bonus_desconto" : null,
               "DataFinal" : "2010-05-12 14:25:42",
               "cliente" : {
                  "PESnome" : "",
                  "PESddd1" : "21",
                  "PESnascdia" : "1981-08-19",
                  "PEStel1" : "",
                  "PEScpf" : "",
                  "PESemail" : ""
               },
               "Valor_desconto" : "0.00",
               "Numero_etiqueta" : null,
               "Lista_casamento" : null,
               "Loja_id" : "155",
               "Revendedor_id" : null,
               "Total" : "110.20",
               "Valor_frete" : "95.80",
               "pagamento" : {
                  "CCCnomecartao" : "Fabiana Campos",
                  "CCCparcela" : "2",
                  "CCCtipo_cartao" : "",
                  "forma_de_pagamento" : "",
                  "CCCnum_cartao" : "",
                  "CCCvalidade_ano" : "2013",
                  "CCCvalidade_mes" : "05"
               },
               "Mobile" : "0",
               "Parceiro" : "-",
               "Esitef" : null,
               "Compra_id" : "1652264",
               "ENTtipofrete" : "S",
               "Status_atual" : "1",
               "Valor_tarifa" : "0.00",
               "enderecos" : {
                  "endereco_cobranca" : {
                     "PEScidade" : "",
                     "PEScep" : "",
                     "PESuf" : "RJ",
                     "PESbairro" : "",
                     "PESlogradouro" : "",
                     "PESnumero" : "313"
                  },
                  "endereco_entrega" : {
                     "ENTbairro" : "",
                     "ENTuf" : "RJ",
                     "ENTcidade" : "",
                     "ENTnumero" : "313",
                     "ENTlogradouro" : "",
                     "ENTcep" : "25080260",
                     "ENTtipo_end" : "Endereço"
                  }
               },
               "Data" : "2010-05-12",
               "Braspag" : null,
               "TpPessoa" : "F",
               "HoraGMT" : "Wed May 12 17:21:03 2010"
            }
         }
      }
    ]
}

I'm using GSON to deserialize.

I've tried:

Type mapType = new TypeToken<Map<Integer, CompraNum> >() {}.getType(); 
Map<Integer, CompraNum> result= gson.fromJson(output, mapType);

In addition they declare in my entity purchasing:

private Map<Integer, CompraNum> compraNum = new HashMap<Integer, CompraNum>();

Here are my classes:

public class Compra {

private Map<Integer, CompraNum> compraNum = new HashMap<Integer, CompraNum>();


public Compra() {
}

public Map<Integer, CompraNum> getCompraNum() {
    return compraNum;
}

public void setCompraNum(Map<Integer, CompraNum> compraNum) {
    this.compraNum = compraNum;
}


}



public class CompraNum {

    @SerializedName("produto")
    @Expose
    private List<Produto> produto = null;
    @SerializedName("Hora")
    @Expose
    private String hora;
    @SerializedName("Valor_entrada")
    @Expose
    private Object valorEntrada;
    @SerializedName("Cielo")
    @Expose
    private Object cielo;
    @SerializedName("Valor_bonus")
    @Expose
    private Object valorBonus;
    @SerializedName("Bonus_desconto")
    @Expose
    private Object bonusDesconto;
    @SerializedName("DataFinal")
    @Expose
    private String dataFinal;
    @SerializedName("cliente")
    @Expose
    private Cliente cliente;
    @SerializedName("Valor_desconto")
    @Expose
    private String valorDesconto;
    @SerializedName("Numero_etiqueta")
    @Expose
    private Object numeroEtiqueta;
    @SerializedName("Lista_casamento")
    @Expose
    private Object listaCasamento;
    @SerializedName("Loja_id")
    @Expose
    private String lojaId;
    @SerializedName("Revendedor_id")
    @Expose
    private Object revendedorId;
    @SerializedName("Total")
    @Expose
    private String total;
    @SerializedName("Valor_frete")
    @Expose
    private String valorFrete;
    @SerializedName("pagamento")
    @Expose
    private Pagamento pagamento;
    @SerializedName("Mobile")
    @Expose
    private String mobile;
    @SerializedName("Parceiro")
    @Expose
    private String parceiro;
    @SerializedName("Esitef")
    @Expose
    private Object esitef;
    @SerializedName("Compra_id")
    @Expose
    private String compraId;
    @SerializedName("ENTtipofrete")
    @Expose
    private String eNTtipofrete;
    @SerializedName("Status_atual")
    @Expose
    private String statusAtual;
    @SerializedName("Valor_tarifa")
    @Expose
    private String valorTarifa;
    @SerializedName("enderecos")
    @Expose
    private Enderecos enderecos;
    @SerializedName("Data")
    @Expose
    private String data;
    @SerializedName("Braspag")
    @Expose
    private Object braspag;
    @SerializedName("TpPessoa")
    @Expose
    private String tpPessoa;
    @SerializedName("HoraGMT")
    @Expose
    private String horaGMT;

   ...

public class PedidoPendente {

@SerializedName("Pedido")
@Expose
private List<Pedido> pedido = null;

public PedidoPendente() {
}

...

public class CompraNum {

@SerializedName("produto")
@Expose
private List<Produto> produto = null;
@SerializedName("Hora")
@Expose
private String hora;
@SerializedName("Valor_entrada")
@Expose
private Object valorEntrada;
@SerializedName("Cielo")
@Expose
private Object cielo;
@SerializedName("Valor_bonus")
@Expose
private Object valorBonus;
@SerializedName("Bonus_desconto")
@Expose
private Object bonusDesconto;
@SerializedName("DataFinal")
@Expose
private String dataFinal;
@SerializedName("cliente")
@Expose
private Cliente cliente;
@SerializedName("Valor_desconto")
@Expose
private String valorDesconto;
@SerializedName("Numero_etiqueta")
@Expose
private Object numeroEtiqueta;
@SerializedName("Lista_casamento")
@Expose
private Object listaCasamento;
@SerializedName("Loja_id")
@Expose
private String lojaId;
@SerializedName("Revendedor_id")
@Expose
private Object revendedorId;
@SerializedName("Total")
@Expose
private String total;
@SerializedName("Valor_frete")
@Expose
private String valorFrete;
@SerializedName("pagamento")
@Expose
private Pagamento pagamento;
@SerializedName("Mobile")
@Expose
private String mobile;
@SerializedName("Parceiro")
@Expose
private String parceiro;
@SerializedName("Esitef")
@Expose
private Object esitef;
@SerializedName("Compra_id")
@Expose
private String compraId;
@SerializedName("ENTtipofrete")
@Expose
private String eNTtipofrete;
@SerializedName("Status_atual")
@Expose
private String statusAtual;
@SerializedName("Valor_tarifa")
@Expose
private String valorTarifa;
@SerializedName("enderecos")
@Expose
private Enderecos enderecos;
@SerializedName("Data")
@Expose
private String data;
@SerializedName("Braspag")
@Expose
private Object braspag;
@SerializedName("TpPessoa")
@Expose
private String tpPessoa;
@SerializedName("HoraGMT")
@Expose
private String horaGMT;

....

Any ideas how to solve it?

    
asked by anonymous 25.07.2017 / 13:41

1 answer

0

I was able to resolve it as follows:

        JSONObject req = new JSONObject(json);
        JSONArray locs = req.getJSONArray("Pedido");
        //JSONArray recs = locs.getJSONArray("record");

        for (int i = 0; i < locs.length(); i++) {
            JSONObject compra = locs.getJSONObject(i);
            Iterator x = compra.keys();

            JSONArray jsonArray = new JSONArray();

            while (x.hasNext()){
                String key = (String) x.next();
                jsonArray.put(compra.get(key));
            }

            for (int a = 0; a < jsonArray.length(); a++) {
                JSONObject compra2 = jsonArray.getJSONObject(a);
                String jsonCompra = compra2.toString();
                //System.out.println(jsonCompra);

                JSONObject jObject = new JSONObject(jsonCompra.trim());
                Iterator<?> keys = jObject.keys();

                while( keys.hasNext() ) {
                    String key = (String)keys.next();
                    if ( jObject.get(key) instanceof JSONObject ) {
                        JSONObject obj = (JSONObject) jObject.get(key);
                        String hr = obj.getString("Hora");
                        System.out.println(hr);
                    }
                }

            }

        }
    
26.07.2017 / 15:08