【问题标题】:Basic JSON formatting using Mule Expression Language使用 Mule 表达式语言的基本 JSON 格式
【发布时间】:2015-06-11 05:33:06
【问题描述】:

向 Slack 上的地址发送 POST 请求时,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:"}

这对我来说看起来有效,但不起作用(也许有一个换行符或其他东西,但即使那样它也只是字符串中的一个字符,不是吗?)

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

【问题讨论】:

  • 你可以转义双引号\"示例文本\"

标签: json mule mule-el


【解决方案1】:

在 Mule 中生成 JSON 的首选方法是创建一个数据结构(如 Map)并使用 json:object-to-json-transformer 将其序列化为 JSON。这需要注意逃逸。

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

【讨论】:

【解决方案2】:

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

https://www.jsoneditoronline.org/

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

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2021-12-22
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多