使用Ruby SDK从SNS接收消息时,AWS SQS JSON格式 [英] AWS SQS JSON format when receiving message from SNS with Ruby SDK

查看:135
本文介绍了使用Ruby SDK从SNS接收消息时,AWS SQS JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已订阅SNS主题的SQS队列.当我向该主题发布新通知时,我使用以下代码(在Sinatra应用程序内):

I have an SQS queue which is subscribed to a SNS topic. When I publish a new notification to the topic, I use the following code (within a Sinatra app):

jsonMessage =  {
    "announcement" => {     
        "first_name" => results['first_name'][:s],  
                        "last_name" => results['last_name'][:s],
                        "loc_code" => results['location'][:s], 
                        "note" => params['note_content']
    }
}

msgid = @announcments_topic.publish(jsonMessage.to_json, 
                                    {subject: "Note Created",
                                     message_structure: 'json' })

当我的队列侦听器收到此通知时,相应哈希的message部分如下所示:

When my queue listener picks up this notification, the message section of the corresponding hash looks like this:

"Message"=>"{\"announcement\":{\"first_name\":\"Eve\",\"last_name\":\"Salt\",\"loc_code\":\"Location\",\"note\":\"test\"}}"

在我的队列侦听器中,我想使用此哈希,但是当我尝试使用

In my queue listener, I want to use this hash, but when I try to use

JSON.parse(result['Message'])

由于转义了双引号,所以出现意外的令牌错误.关于如何解决此问题的任何建议?我是否无法以JSON格式正确发送通知?我怎样才能使sns/sqs不转义双引号?

I get an unexpected token error because of the escaped double quotes. Any suggestions on how I can fix this? Am I not sending my notification as JSON properly? How can I get sns/sqs to not escape the double quotes?

推荐答案

找到了答案.

问题是我获取JSON的方式.我需要使用JSON.load(result['Message']),而不是JSON.parse(...).

The problem was the way I was getting the JSON. I needed to use JSON.load(result['Message']), instead of JSON.parse(...).

这篇关于使用Ruby SDK从SNS接收消息时,AWS SQS JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆