我想使用块布局格式向我的 Slack 应用发送消息。我在PHP中创建了一个关联数组,然后使用json_encode()将其转换为JSON。问题是它没有转换为松弛所期望的 JSON 格式,并且我收到错误"无效块格式"。这是我的代码,输出和 slack 预期的输出。
$data = array(
'blocks' => array(
'type' => 'mrkdwn',
'text' => 'Danny Torrence left the following review for your property'
),
);
$data = json_encode($data);
我得到以下输出:
{"blocks":{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}}
但是,Slack 需要以下格式的 JSON:
{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}
我只需要最后将一个"{"转换为">[",将一个"}"转换为"]"。我将不胜感激任何帮助。
谢谢
我没有足够的声誉,但我相信这是重复的: 没有方括号 json 数组
另外,这不是有效的json,您可以检查 https://jsonlint.com/?code=
{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}
总结一下这篇文章,你真正需要做的就是用另一个数组包装你的内部数组
$data = array(
'blocks' => array(array(
'type' => 'mrkdwn',
'text' => 'Danny Torrence left the following review for your property'
)),
);
这将返回:
{"blocks":[{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}]}