Grab object inside JSON string

Asked

Viewed 535 times

1

I receive 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”
}
}`

And I need to take the following data from this json "Sintese Cadastral", "Pendenciasbacen", "Financial pendencias" and put in objects of their respective classes. Does anyone have any idea how I can do this? Using any of the many libraries that exist to parse json.

1 answer

0


You can use a library called Jackson, which serializes (object for json) and deserializes (json for object):

Documentation by Jackson

Basically, you need to:

1) Add Jackson’s 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 jackson’s.

A small example that serves for you to understand and follow your need. The Data class has some attributes, among them a Department class. Here’s json:

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

The following is the class mapping that reflects the json structure. See the annotation on the Date type variable, to tell the json 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 on a Data object:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.