Olá pessoal, por favor alguém pode me ajudar? Até agora lidava com objetos json simples, sem problemas, mas estou tentando pegar propriedade de um objeto da AWS, de recebimento de email e não estou conseguindo. Segue o o código:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class PegarPropriedadeJson {
public static void main(String[] args) {
String stringJson = "{\"notificationType\":\"Received\",\"mail\":{\"timestamp\":\"2022-10-18T23:53:59.219Z\",\"source\":\"roberto@redeestado.com.br\",\"messageId\":\"vofc7uok9fn2q3im6l01e4rd6na6m2a6v079p8g1\",\"destination\":[\"rute@redecnh.com.br\"],\"headersTruncated\":false,\"headers\":[{\"name\":\"Return-Path\",\"value\":\"<roberto@redeestado.com.br>\"},{\"name\":\"Received\",\"value\":\"from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by inbound-smtp.us-east-1.amazonaws.com with SMTP id vofc7uok9fn2q3im6l01e4rd6na6m2a6v079p8g1 for rute@redecnh.com.br; Tue, 18 Oct 2022 23:53:59 +0000 (UTC)\"},{\"name\":\"X-SES-Spam-Verdict\",\"value\":\"PASS\"},{\"name\":\"X-SES-Virus-Verdict\",\"value\":\"PASS\"},{\"name\":\"Received-SPF\",\"value\":\"pass (spfCheck: domain of redeestado.com.br designates 209.85.208.48 as permitted sender) client-ip=209.85.208.48; envelope-from=roberto@redeestado.com.br; helo=mail-ed1-f48.google.com;\"},{\"name\":\"Authentication-Results\",\"value\":\"amazonses.com; spf=pass (spfCheck: domain of redeestado.com.br designates 209.85.208.48 as permitted sender) client-ip=209.85.208.48; envelope-from=roberto@redeestado.com.br; helo=mail-ed1-f48.google.com; dkim=pass header.i=@redeestado-com-br.20210112.gappssmtp.com; dmarc=none header.from=redeestado.com.br;\"},{\"name\":\"X-SES-RECEIPT\",\"value\":\"AEFBQUFBQUFBQUFGWE4rUnBhZlRHSzFNZHFYZkZXd01DY2pRVjZqbkswTHNHUndiM0VMRjFGVnNHbXNtQVo0UDdDSmxoeEdUZGpMT3FDaEowMHdCQTk1UEExTzJHMjFqeXVmZkcrR0RYSGFWUVl5Mzlla2RoZVR5alo5Y0JUeWlTOTA0UWplbndhN0xBbTYwditkaitVbG4vR2tYeVVtY2dNRzgzY0V3bHNzeDArY1d4RDJKeTduYVpxeXF3QzRXTEIydC9BbkU3SFNNQy91d0NQK3FpZ2dqdG81Qm83MzJVUlNjZnR3UVZ6V1NaMnY5Z0tjMnVENEVhUlpTS3RrVnk3TjU0Z0luWU5DZ0tTRHdlOWEyS3FHdGJyYTZGUytGajZUc2VLRExmV01WRXJRWExRbjNDa1E9PQ==\"},{\"name\":\"X-SES-DKIM-SIGNATURE\",\"value\":\"a=rsa-sha256; q=dns/txt; ... TIVE QUE APAGAR O RESTANTE POIS ULTRAPASSOU O LIMITE.
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement json = gson.fromJson(stringJson, JsonElement.class);
JsonObject jsonObjeto = (JsonObject) json;
System.out.println(jsonObjeto.get("Message").getAsJsonObject().get("mail").getAsJsonObject().get("source"));
}
}
O eclipse exibe o seguinte erro:
Exception in thread "main" java.lang.IllegalStateException: Not a JSON Object:
Já validei o objeto no site: https://jsonlint.com/.
Valid JSON.
Agradeço a ajuda, já apanhei bastante no google, mas não consegui.