C#将字符串转换为JSON

C#将字符串转换为JSON,c#,json,C#,Json,我正试图使用C#来发布一个JSON负载,我可以实现这个负载,但是我很难理解如何用我自己的字符串替换示例字符串 从下面的代码中,您可以看到我有一个字符串要发送到我的Web链接。当我对StringContent使用“{\”text\”:\“Hello,World!\”}”时,代码运行良好,但如果我尝试用output\u message的字符串替换它,则代码无法工作。我正在尝试解决如何将输出消息转换为JSON可以识别的格式 { string output_message = "

我正试图使用C#来发布一个JSON负载,我可以实现这个负载,但是我很难理解如何用我自己的字符串替换示例字符串

从下面的代码中,您可以看到我有一个字符串要发送到我的Web链接。当我对StringContent使用“{\”text\”:\“Hello,World!\”}”时,代码运行良好,但如果我尝试用output\u message的字符串替换它,则代码无法工作。我正在尝试解决如何将输出消息转换为JSON可以识别的格式

    {
        string output_message = "The file " + filename + " has been modified by " + user_modified + " and moved to the " + file_state + " file state. Please review the " + filename + " file and approve or reject.";            
        PostWebHookAsync(output_message);
        Console.ReadLine();
    }
    static async void PostWebHookAsync(string Aoutput_message)
    {
        using (var httpClient = new HttpClient())
        {
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "www.mywebsitelink"))
            {
                //request.Content = new StringContent("{\"text\":\"Hello, World!\"}", Encoding.UTF8, "application/json");  // - original do not delete
                request.Content = new StringContent(Aoutput_message, Encoding.UTF8, "application/json");
                var response = await httpClient.SendAsync(request);
               Console.WriteLine(response.StatusCode);
               Console.WriteLine(response.Content);
            }
        }
    }

我想用字符串替换“{\“text\”:\“Hello,World!\“}”,最好的方法是创建一个对象并将其序列化

要使用
JavaScriptSerializer
,必须添加对
System.Web.Extensions.dll的引用

因此,针对您的问题,我们创建了一个匿名对象,其中包含一个属性文本,我们传递值
aooutput\u message

static async void PostWebHookAsync(string Aoutput_message)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "www.mywebsitelink"))
        {
            //request.Content = new StringContent("{\"text\":\"Hello, World!\"}", Encoding.UTF8, "application/json");  // - original do not delete

            string jsonValue = new JavaScriptSerializer().Serialize(new
            {
                text = Aoutput_message,
            });

            request.Content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = await httpClient.SendAsync(request);
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content);
        }
    }
}

最好的方法是创建一个对象并将其序列化

要使用
JavaScriptSerializer
,必须添加对
System.Web.Extensions.dll的引用

因此,针对您的问题,我们创建了一个匿名对象,其中包含一个属性文本,我们传递值
aooutput\u message

static async void PostWebHookAsync(string Aoutput_message)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "www.mywebsitelink"))
        {
            //request.Content = new StringContent("{\"text\":\"Hello, World!\"}", Encoding.UTF8, "application/json");  // - original do not delete

            string jsonValue = new JavaScriptSerializer().Serialize(new
            {
                text = Aoutput_message,
            });

            request.Content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = await httpClient.SendAsync(request);
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content);
        }
    }
}

据我所知,人们正在从JavaScriptSerializer转向Json.NET。文档中甚至推荐了它

相应的Json.NET代码如下所示:

static async void PostWebHookAsync(string output_message)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "www.mywebsitelink"))
        {
            string jsonValue = JsonConvert.SerializeObject(new
            {
                text = output_message
            });
            request.Content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = await httpClient.SendAsync(request);
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content);
        }
    }
}

要使用Json.NET,您需要安装nuget软件包。

据我所知,人们正在从JavaScriptSerializer转向Json.NET。文档中甚至推荐了它

相应的Json.NET代码如下所示:

static async void PostWebHookAsync(string output_message)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "www.mywebsitelink"))
        {
            string jsonValue = JsonConvert.SerializeObject(new
            {
                text = output_message
            });
            request.Content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = await httpClient.SendAsync(request);
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content);
        }
    }
}

要使用Json.NET,您需要安装nuget包。

Json代表JavaScript对象表示法,因此它有一些验证规则。在您的情况下,您可以发送
$“\{\\”text\”:“{output\\\\\\\\\\\\\\”
$“\{\\\”value\”:“{output\\\\\\\\\\\\\”
JSON的可能副本代表JavaScript对象表示法,因此它有一些验证规则。在您的情况下,您可以发送
$“\{\\”text\”:“{output\\\\\\\\\\\\\\”
$“\{\\\”value\”:“{output\\\\\\\\\\\\\\\\\\\\\\\”
这也可以重复,谢谢您的反馈。与JavaScriptSerializer().Serialize函数相比,使用此函数有什么好处?@AWH:JavaScriptSerializer在.NET核心中不受支持,因此为了使其“经得起未来考验”,您应该使用Json.NET。当反序列化时,我发现Json.NET更容易使用,对于序列化,它是相同的。这同样有效,感谢您的反馈。与JavaScriptSerializer().Serialize函数相比,使用此函数有什么好处?@AWH:JavaScriptSerializer在.NET核心中不受支持,因此为了使其“经得起未来考验”,您应该使用Json.NET。当反序列化时,我发现Json.NET更容易使用,对于序列化来说也是一样的。