使用Mule表达式语言的基本JSON格式

时间:2015-06-10 14:41:48

标签: json mule mule-el

当发送POST请求到Slack上的地址时,JSON需要格式化为:

{"channel": "#testing", "username": "test", "text": "Subject"}

或类似的东西,这是正常的。我遇到的问题非常基本,但基本上,我需要发送一些通过JSON从电子邮件中获取的详细信息,如下所示:

{"channel": "#testing", "username": "#[message.inboundProperties['From']]", "text": "Subject"}

当我在此之后记录有效载荷时,我得到:

{"channel": "#testing", "username": ""Doe, John" <email@address.com>", "text": "Subject"}

这会抛出一个500错误代码,大概是因为它不再是有效的JSON(我的猜测是因为奇怪的引用,但是如果我做了其他事情并做了类似的事情:

{"channel": "#testing", "username": "username", "text": "#[message.payload]"}

给我这个:

{"channel": "#testing", "username": "username", "text": "email body
", "icon_emoji": ":man_with_turban:"}

哪个看起来对我有用,但是不起作用(也许还有一个新的字符或其他东西,但即便如此,它只是字符串中的一个字符,不是吗?)

我不确定如何正确地做到这一点。

2 个答案:

答案 0 :(得分:3)

在Mule中生成JSON的首选方法是创建数据结构(如Map)并使用json:object-to-json-transformer将其序列化为JSON。这可以解决问题。

如果您坚持将JSON创建为字符串,则需要正确转义所注入的值(例如,使用escapeJavaScript)。

答案 1 :(得分:0)

您可以使用此页面验证JSON:

https://www.jsoneditoronline.org/

{"channel": "#testing", "username": "username", "text": "email body", "icon_emoji": ":man_with_turban:"}
相关问题