在 JSON 对象中发送数组时出错

Error sending an array within a JSON object

为了使用 Iris Go

发送此 JSON
{
  "response_type": "in_channel",
  "text": "It's 80 degrees right now.",
  "attachments": [
    {
        "text":"Partly cloudy today and tomorrow"
    }
  ]
}

我正在尝试,但没有用

ctx.JSON(iris.Map{
    "text": "It's 80 degrees right now.",
    "response_type": "in_channel",
    "attachments":[{ "text": "Partly cloudy today and tomorrow" }],
})

因为附件行出现如下错误

syntax error: unexpected {, expecting expression

你知道怎么解决吗?

在您的 Go 代码中,您没有为数组或其元素指定类型。假设您希望它是 iris.Map:

ctx.JSON(iris.Map{
    "text": "It's 80 degrees right now.",
    "response_type": "in_channel",
    "attachments": []iris.Map{iris.Map{ "text": "Partly cloudy today and tomorrow" }},
})