Paste object inside String JSON

1

I get the following JSON from a web service /

'{
“SinteseCadastral”: {
“Documento”: “99999999999”,
“Nome”: “NOM DE TESTES”,
“NomeMae”: “NOME DA MAE DE TESTE”,
“NomeFantasia”: “NOME FANTASIA DE TESTE”,
“DataNascimento”: “99/99/9999”,
“DataFundacao”: “99/99/9999”,
“SituacaoRFB”: “REGULAR”,
“SituacaoDescricaoRFB”: “REGULAR”,
“DataSituacaoRFB”: “29/04/2018 12:04:34”
},
“TotalOcorrencias”: 3,
“ValorTotalOcorrencias”: “99.999,99”,
“AlertaDocumentos”: {
“NumeroMensagem”: “1”,
“TotalMensagens”: “1”,
“TipoDocumento”: “RG”,
“NumeroDocumento”: “9999999”,
“MotivoOcorrencia”: “EXTRAVIO”,
“DataOcorrencia”: “01/01/1980”,
“TelefonesContato”: [
{
“Telefone”: “(11) 9999-9999”
}
],
“Mensagem”: “Alerta”
},
“PendenciasInternas”: {
“TotalOcorrencias”: 0,
“Mensagem”: “NAO CONSTAM OCORRENCIAS”
},
“PendenciasFinanceiras”: {
“TotalOcorrencias”: 0,
“OcorrenciaMaisAntiga”: “99/9999”,
“OcorrenciaMaisRecente”: “99/9999”,
“PendenciasFinanceirasDetalhe”: [
{
“DataOcorrencia”: “99/99/9999”,
“Modalidade”: “OO”,
“Avalista”: “N”,
“TipoMoeda”: “R$”,
“Valor”: “99.999,99”,
“Contrato”: “99999999999999”,
“Origem”: “UF”,
“Sigla”: “XX”,
“SubJudice”: “N”,
“SubJudiceDescricao”: “”,
“TipoAnotacao”: “PEFIN”,
“TipoAnotacaoDescricao”: “PENDENCIA FINANCEIRA”
}
],
“Mensagem”: “CONSTAM RESTRICOES”
},
“PendenciasBacen”: {
“TotalOcorrencias”: 9,
“OcorrenciaMaisAntiga”: “99/9999”,
“OcorrenciaMaisRecente”: “99/9999”,
“Banco”: “999”,
“Agencia”: “9999”,
“NomeFantasiaBanco”: “NOME FANTASIA DO BANCO”,
“PendenciasBacenDetalhe”: [
{
“DataOcorrencia”: “99/99/9999”,
“NumeroCheque”: “9999999”,
“AlineaCheque”: “99”,
“QuantidadeCCFBanco”: “9”,
“Valor”: “99.999,99”,
“Banco”: “999”,
“NomeBanco”: “NOME FANTASIA DO BANCO”,
“Agencia”: “999”,
“Cidade”: “CIDADE”,
“UF”: “UF”
}
],
“Mensagem”: “EXISTEM RESTRICOES”
},
“RiskScore”: {},
“LimiteCredito”: {},
“Mensagem”: “Transacao realizada com sucesso”,
“Status”: true,
“Transacao”: {
“Status”: true,
“CodigoStatus”: “G000M001”,
“CodigoStatusDescricao”: “Transacao realizada com sucesso”
}
}'

You need to get the following data from this json "Cadastral Synthesis", "PendencesBacen", "Financial Pending" and put into objects of their respective classes. Does anyone have any idea how I can do this? Using any of the various libraries that exist for json parse.

    
asked by anonymous 29.04.2018 / 22:58

1 answer

0

You can use a library called Jackson, which does the serialization (object to json) and deserialization (json to object):

Jackson Documentation

Basically, you need:

1) Add the Jackson jar to your project.

2) Create a class mapping that reflects your json, using annotations to direct the deserialization process.

3) Deserialize using the class ObjectMapper of Jackson.

A small example for you to understand and follow up on your need. The Data class has some attributes, among them a Department class. Here is json:

{
  "nome":"jose da silva",
  "idade": 40,
  "data": "25-04-2018",
  "departamento": {
    "codigo": "alfa"
  }
}

Next, the class mapping that reflects the json structure. See the note about the variable of type Date, to inform json of the expected format:

class Dados {

    private String nome;
    private int idade;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date data;

    private Departamento departamento;
    //getters e setters

}

class Departamento {
    private String codigo;
    //getters e setters

Finally, the code to effectively deserialize json into an object of type Data:

Dados dados = new ObjectMapper().readValue(json, Dados.class);

If this answer helped you, mark it as the correct one, okay?

    
30.04.2018 / 03:42