摘要JSON消息有效负载

时间:2017-08-07 01:54:59

标签: java json amazon-web-services webhooks

我对JSON并不熟悉,也不确定这是否可行,

背景: 我使用AWS云监控并使用AWS SNS发送DHCP端点,即Twilio。通知很好,在Hipchat中按预期工作,简明扼要地总结

[ALARM] saas-ajtest-mem-gt-80-5min Threshold Crossed: 2 datapoints [47.9 (07/08/17 01:10:00), 47.9 (07/08/17 01:05:00)] were greater than or equal to the threshold (30.0).

但是当它被发送到我的Twilio号码时,我将收到所有的消息方式太长时间。

Type:  "Notification"
   MessageId" : "27684767-bf13-5e0f-b02d-15667f98e595"
 TopicArn:   "arn:aws:sns:us-east-1:00000000:critical_twilio"
  Subject:   "ALARM: "saas-ajtest-mem-gt-80-5min" in US East (N. Virginia)"
   Message: {"AlarmName":"saas-ajtest-mem-gt-80-5min""AlarmDescription":null"AWSAccountId":"00000000""NewStateValue":"ALARM""NewStateReason":"Threshold Crossed: 2 datapoints [47.9 (07/08/17 01:10:00) 47.9 (07/08/17 01:05:00)] were greater than or equal to the threshold (30.0).""StateChangeTime":"2017-08-07T01:11:39.708+0000""Region":"US East (N. Virginia)""OldStateValue":"OK""Trigger":{"MetricName":"MemoryUtilization""Namespace":"test""StatisticType":"Statistic""Statistic":"AVERAGE""Unit":null"Dimensions":[{"name":"ContainerName""value":"ajtest"}]"Period":60"EvaluationPeriods":5"ComparisonOperator":"GreaterThanOrEqualToThreshold""Threshold":30.0"TreatMissingData":"""EvaluateLowSampleCountPercentile":""}}"

我想要做的只是获取AlarmName,Threshold Crossed,Dimensions名称。基本上我想要一个类似于hipchat的输出。

This my sample groovy script

def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText( body )
def type = result.Type
def messageID = result.MessageId
def subject = result.Subject
def message = result.Message
def alldata = type + messageID + subject + message

尝试使用

def message = result.Message.AlarmName
def message = result.Message."AlarmName"

但它只会返回null。

任何输入都将不胜感激。提前感谢你:))

1 个答案:

答案 0 :(得分:0)

实际上找到了一个解决方案,我已经使用了地图

Map<String, String> mappedResult = (Map<String, String>) slurper.parseText( message )

这将得到另一个json:)