Last active
August 26, 2021 19:09
-
-
Save david-botelho-mariano/6cc9325235f8adfbfd96cc578a4ef6f0 to your computer and use it in GitHub Desktop.
parse nested json without creating class using java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<dependency> | |
//<groupId>com.fasterxml.jackson.core</groupId> | |
//<artifactId>jackson-databind</artifactId> | |
//<version>2.13.0-rc1</version> | |
//</dependency> | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class JsonParser { | |
public static void main(String[] args) throws JsonMappingException, JsonProcessingException { | |
String texto = "{\r\n" | |
+ " \"msg\":\"Autenticacao realizada com sucesso\",\r\n" | |
+ " \"success\":true,\r\n" | |
+ " \"validacaoAssinatura\":{\r\n" | |
+ " \"cpfConfirmado\":false,\r\n" | |
+ " \"dados\":{\r\n" | |
+ " \"cnpj\":null,\r\n" | |
+ " \"cpf\":\"11122333\",\r\n" | |
+ " \"nomeEmpresarialPJ\":null,\r\n" | |
+ " \"nomeResponsavel\":null,\r\n" | |
+ " \"numeroRegistroOAB\":null\r\n" | |
+ " },\r\n" | |
+ " \"desafioValido\":false,\r\n" | |
+ " \"documentoAttached\":true,\r\n" | |
+ " \"hashDocumentoValido\":true,\r\n" | |
+ " \"integridade\":true,\r\n" | |
+ " \"ipdesafioValido\":false,\r\n" | |
+ " \"validade\":true\r\n" | |
+ " }\r\n" | |
+ "}"; | |
JsonNode respostaJson = new ObjectMapper().readTree(texto); | |
String cpf = respostaJson.get("validacaoAssinatura").get("dados").get("cpf").asText(); | |
System.out.println(cpf); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment