如何从Json Node中提取特定值?

时间:2018-02-16 10:32:58

标签: java json

您好我输入JsonNode类型如下:

{
    "Records": [
        {
            "EventSource": "aws:sns",
            "EventVersion": "1.0",
            "EventSubscriptionArn": "arn:aws:sns:us-west-1:270252992114:d1dd43d0-8bf1-496c-bc94-f4e70dec5032",
            "Sns": {
                "Type": "Notification",
                "MessageId": "923d528f-c844-5d49-95de-018f6d583bfd",
                "TopicArn": "arn:aws:sns:us-west-1:270252992114:",
                "Subject": null,
                "Message": "AAAAAQAAAAAAAAABAAAAJGNvbS5hbWF6b24uY3VzdG9tZXJldmVudC5jZXMudGVzdC5OQQAAABDW7HxyIs4PvuXtarSxIxdeAAABALZT+4Yv09z9tK48/miZ6IhjvVCLixiH3GLltCxQzEkNgf9NTD57G744vdnw1D3LxLmxT4i0CCkVJJ5IPaH3Ud5Oy6MKa5wRrldtS6MTDMzKqoPwcdMiLm3AogonUAqyBYrxWt8fHrCYIlggX9EFW9Dk9ugDRrJuhCZqfVnt/wRUyP33DycwRKHBHSxn1XuDi9ZyEeCxrMLn9rGmpVvUQb3mdFReoVDmP4cEWcQEwYxG6/WvyimHGD5JtSxzlCR3rwddu8vXlp9Mq127+scPipAm83IJSw7CS5laZQZAD8qR4WwSfSZw8kq72y/LulhzWj3w7jbtOtmhbdnPyIN+L4I=",
                "Timestamp": "2018-02-16T09:22:36.149Z",
                "SignatureVersion": "1",
                "Signature": "f+23JQ6r9gZL6dRay4wqJHrX5CnB9cSVpPS/zgopPKHFOD5zvEpuTiGfuNf0e2L6/84pm6gK9xCEakzeaWtBAp7J/hFfbKQ2BSJ/GAKX1peG16Q8TS2k0NVxjzG4ImEHxf3i3ODOFJeA7WHxRZiMkNO+79lZDxkkOQdfWR3OEQ1yP8CjE4HLlLoSEUdk170AOw1nty9NZ6FOnsotLf5jce0GrXs1lkn7J/3nv/YlSqdMZEAR4SZDELCH3krQ4mUO7gwUfkDSFXsjWLarTayYl20eH3g/RZLQgPEQGTRBZW5wrknXg1vm4H4ICxxrGJBooAs7BCLkUVT1m4juRrlljA==",
                "SigningCertUrl": "https://sns.us-west-1.amazonaws.com/SimpleNotificationService-433026a4050d206028891664da859041.pem",
                "UnsubscribeUrl": "https://sns.us-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-1:270252992114:",
                "MessageAttributes": {}
            }
        }
    ]
}

我想提取TopicArn和Message的值,我尝试了以下代码,但rawSns为null:

JsonNode rawResource = input.get("Records");
logger.log("rawResource is: " + rawResource);
JsonNode rawSns = rawResource.get("Sns");

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

JsonNode rawResource = input.get("Records");
logger.log("rawResource is: " + rawResource);
JsonNode rawSns = rawResource.get(0).get("Sns");

请参阅here for Javadoc

答案 1 :(得分:0)

这正是您所需要的。

var jsonObject = JSON.parse('{"Records":[{"EventSource":"aws:sns","EventVersion":"1.0","EventSubscriptionArn":"arn:aws:sns:us-west-1:270252992114:d1dd43d0-8bf1-496c-bc94-f4e70dec5032","Sns":{"Type":"Notification","MessageId":"923d528f-c844-5d49-95de-018f6d583bfd","TopicArn":"arn:aws:sns:us-west-1:270252992114:","Subject":null,"Message":"AAAAAQAAAAAAAAABAAAAJGNvbS5hbWF6b24uY3VzdG9tZXJldmVudC5jZXMudGVzdC5OQQAAABDW7HxyIs4PvuXtarSxIxdeAAABALZT+4Yv09z9tK48/miZ6IhjvVCLixiH3GLltCxQzEkNgf9NTD57G744vdnw1D3LxLmxT4i0CCkVJJ5IPaH3Ud5Oy6MKa5wRrldtS6MTDMzKqoPwcdMiLm3AogonUAqyBYrxWt8fHrCYIlggX9EFW9Dk9ugDRrJuhCZqfVnt/wRUyP33DycwRKHBHSxn1XuDi9ZyEeCxrMLn9rGmpVvUQb3mdFReoVDmP4cEWcQEwYxG6/WvyimHGD5JtSxzlCR3rwddu8vXlp9Mq127+scPipAm83IJSw7CS5laZQZAD8qR4WwSfSZw8kq72y/LulhzWj3w7jbtOtmhbdnPyIN+L4I=","Timestamp":"2018-02-16T09:22:36.149Z","SignatureVersion":"1","Signature":"f+23JQ6r9gZL6dRay4wqJHrX5CnB9cSVpPS/zgopPKHFOD5zvEpuTiGfuNf0e2L6/84pm6gK9xCEakzeaWtBAp7J/hFfbKQ2BSJ/GAKX1peG16Q8TS2k0NVxjzG4ImEHxf3i3ODOFJeA7WHxRZiMkNO+79lZDxkkOQdfWR3OEQ1yP8CjE4HLlLoSEUdk170AOw1nty9NZ6FOnsotLf5jce0GrXs1lkn7J/3nv/YlSqdMZEAR4SZDELCH3krQ4mUO7gwUfkDSFXsjWLarTayYl20eH3g/RZLQgPEQGTRBZW5wrknXg1vm4H4ICxxrGJBooAs7BCLkUVT1m4juRrlljA==","SigningCertUrl":"https://sns.us-west-1.amazonaws.com/SimpleNotificationService-433026a4050d206028891664da859041.pem","UnsubscribeUrl":"https://sns.us-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-1:270252992114:","MessageAttributes":{}}}]}');

console.log(((jsonObject["Records"][0])["Sns"])["TopicArn"]);

此在线工具可以帮助您了解JSON的结构:JSON viewer