C# 将HTTP POST请求的字符串转换为JSON

C# 将HTTP POST请求的字符串转换为JSON,c#,json,asp.net-mvc,slack,C#,Json,Asp.net Mvc,Slack,我试图从Slack的HTTP POST请求返回一个json文件。我正在使用netcoreapp3.1和Newtonsoft.Json NuGet。现在,我的HTTP POST函数如下所示 public async Task<ActionResult> SlashCommand([FromForm] SlackSlashCommand request) { var retVal = new JsonResult(GetBlock());

我试图从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;
    }
public异步任务SlashCommand([FromForm]SlackSlashCommand请求)
{
var retVal=newjsonresult(GetBlock());
返回返回;
}
GetBlock()是一个返回我创建的类的函数。目前这是可行的,但每次我想修改它返回的json时,我都必须修改该类。我非常希望有一个字符串格式的json,我可以复制并粘贴到我的代码中,然后以json格式返回Slack

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

有什么帮助吗?谢谢。

我找到了答案

这是我的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;
}
public异步任务SlashCommand([FromForm]SlackSlashCommand请求)
{
var retVal=newJSONResult(JsonConvert.DeserializeObject(str));
返回返回;
}

这很有效。对不起,如果我没有提供太多的信息

另一种选择是使用匿名类型,该类型不易成为无效JSON(JSON字符串中的简单键入可能会导致整个JSON块无法读取):

产生:

{
    "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"
                }
            ]
        }
    ]
}

修改JSON与修改字符串格式有何不同?老实说,如果您阅读了一些关于字符串、JSON、什么是JSON、类以及序列化和反序列化的文档,这将对您大有帮助。你的问题很一般,没有太多的信息来回答。请提供一个最小的可重复的代码示例。再次完成工作所需的所有方法和代码谢谢。这是真的,我需要阅读更多关于这方面的内容。所以你想每次都发送一条固定的JSON消息吗?您永远不想动态更改它,等等?正确,因为每次都是相同的消息。这就是为什么转换字符串是最简单的方法。
{
    "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"
                }
            ]
        }
    ]
}