How to convert JSON to Object and find an id - JAVA

Asked

Viewed 800 times

3

I need to convert a JSON into an object and navigate to an ID and capture only it. JSON Example:

"{"_status":"sucesso","_dados":{"_sucesso":[{"idintegracao":"BkmRboZmaaa","situacao":"SALVO","TituloNumeroDocumento":"01012020","TituloNossoNumero":"501","CedenteContaCodigoBanco":"748","CedenteContaNumero":"9999","CedenteConvenioNumero":"9999"}],"_falha":[]}}

I need to capture the idIntegration that is in _data. _success[0]. idintegracao, and save in a string. What I’ve done so far:

 String jsonString = response.body().string();

        JSONObject jsonObjectBoleto = new JSONObject("{" + jsonString + "}");


        Iterator<String> iteratorBoletos = jsonObjectBoleto.keys();
        System.out.println("TO AQUI");

        System.out.println("TO AQUI NO WHILE " + iteratorBoletos.toString());
        JSONObject dadosBoleto = jsonObjectBoleto.getJSONObject(iteratorBoletos.toString());
        System.out.println("TO AQUI NO WHILE 2 " + iteratorBoletos.toString());
        Boleto boleto = new Boleto();
        System.out.println("DADOS DO BOLETO: " + dadosBoleto.getString("idintegracao"));

I tried to do it too but without success:

      String jsonString = response.body().string();

        String json = ("{" + jsonString + "}");

        JSONObject obj1 = new JSONObject(json);

        String idIntegracao = obj1.getString("idintegracao");

        System.out.println("idIntegracao : " + idIntegracao);
  • 1

    You must have searched for libs like Jackson and Gson, and you have some specific question, don’t you? What would it be? You can post the code of what you have done so far?

  • 1

    Possible duplicate of Grab object inside JSON string

  • 1

    You already know which JSON API you will use?

  • You are using Spring Boot?

  • To generate Java classes, you can use a JSON to Java Classes generator. http://json2java.azurewebsites.net/

  • I posted what I’ve done so far

Show 1 more comment

1 answer

1


You can use the Json processing API available from Java 1.8

Dependencies Maven:

<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>1.1</version>
 </dependency>
 <dependency>
     <groupId>org.glassfish</groupId>
     <artifactId>javax.json</artifactId>
     <version>1.1</version>
</dependency>

After adding the libraries retrieve Json in a String and create a Jsonreader to read the String:

String jsonString = response.readEntity(String.class);
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));

Retrieve Jsonobject from jsonReader:

 JsonObject jsonObject = jsonReader.readObject();

Recover Jsonobject for "_data attribute"

 JsonObject attributeDados = jsonObject.getJsonObject("_dados");

Retrieve Jsonarray for the "_success attribute"

 JsonArray jsonArray = attributeDados.getJsonArray("_sucesso");

Finally recover the id

String idIntegracao = jsonArray.getJsonObject(0).getString("idintegracao");
  • Whoops, thanks for the reply, but I had a nullPointer in this line javax.json.Jsonarray jsonArray = attributeDados.getJsonArray("_success");

  • While testing I checked that the error is on the line where it retrieves the id, replace "getJsonObject(1)" with "getJsonObject(0)"

  • I made the change plus the error insists on the top line, where the _success.

  • Guy Json was passing null, I checked and it worked, thank you very much :D

Browser other questions tagged

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