【问题标题】:Converting a string to a JSON for a HTTP POST request为 HTTP POST 请求将字符串转换为 JSON
【发布时间】:2020-01-29 00:59:31
【问题描述】:

我正在尝试从 Slack 的 HTTP POST 请求中返回一个 json 文件。我正在使用 netcoreapp3.1 和 Newtonsoft.Json NuGet。现在我的 HTTP POST 函数看起来像这样。

    public async Task<ActionResult> SlashCommand([FromForm] SlackSlashCommand request)
    {
        var retVal = new JsonResult(GetBlock());

        return retVal;
    }

GetBlock() 是一个返回我创建的类的函数。这目前有效,但每次我想修改它返回的 json 时,我都必须修改那个类。我真的很想拥有一个字符串格式的 json,我可以将它复制并粘贴到我的代码中,然后以 json 格式返回到 Slack。

有没有办法做到这一点?我一直在尝试使用 JsonConvert.DeserializeObject(str);但我使用不正确。据我了解,该函数接受一个字符串并将其转换为一个对象。我需要接受一个字符串并将其转换为 Microsoft.AspNetCore.Mvc.ActionResult json。

有什么帮助吗?谢谢。

【问题讨论】:

  • 修改 JSON 与修改字符串格式有何不同?
  • 老实说,如果您阅读一些关于字符串、json、什么是 json、类以及最后,序列化和反序列化意味着什么的文档,它将对您有很大帮助。你的问题很笼统,没有太多信息可以给你答案
  • 请务必提供您的代码的最小可重现示例。再次完成工作所需的所有方法和代码
  • 谢谢。这是真的,我需要阅读更多内容。

标签: c# json asp.net-mvc slack


【解决方案1】:

另一种选择是使用匿名类型,这将不太容易成为无效的 JSON(JSON 字符串中的简单错字可能会导致整个 JSON 块不可读):

var data = new
{
    blocks = new object[] {
            new {
                type = "section",
                text = new {
                    type = "plain_text",
                    text = "Hello!",
                    emoji = true
                }
            },
            new {
                type = "divider"
            },
            new {
                type = "actions",
                elements = new object[] {
                    new {
                        type = "button",
                        text = new {
                            type = "plain_text",
                            text = "Help"
                        },
                        value = "helpButton"
                    }
                }
            }
        }
};

return new JsonResult(data);

生产:

{
    "blocks": [
        {
            "type": "section",
            "text":
            {
                "type": "plain_text",
                "text": "Hello!",
                "emoji": true
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text":
                    {
                        "type": "plain_text",
                        "text": "help"
                    },
                    "value": "helpButton"
                }
            ]
        }
    ]
}

Try it online

【讨论】:

    【解决方案2】:

    我找到了答案。

    这是我的字符串格式的 JSON:

    string str = "{\"blocks\": [{\"type\": \"section\",\"text\": {\"type\": \"plain_text\",\"text\": \"Hello!\",\"emoji\": true}},{\"type\": \"divider\"},{\"type\": \"actions\",\"elements\": [{\"type\": \"button\",\"text\": {\"type\": \"plain_text\",\"text\": \"Help\"},\"value\": \"helpButton\"}]}]}";
    

    然后这是我的功能:

    public async Task<ActionResult> SlashCommand([FromForm] SlackSlashCommand request)
    {
        var retVal = new JsonResult(JsonConvert.DeserializeObject<object>(str));
    
        return retVal;
    }
    

    这很好用。抱歉,如果我没有提供太多信息。

    【讨论】:

    • 所以你想每次都发送固定的 JSON 消息?您永远不想动态更改它等?
    • 正确,因为每次都是相同的消息。这就是为什么转换字符串是最简单的方法。
    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多