C#将字符串转换为JSON

时间:2019-05-13 10:42:10

标签: c# json

我正在尝试使用C#POST到可以实现的JSON有效负载,但是我在理解如何用我自己的字符串替换示例字符串方面遇到困难。

从下面的代码中,您可以看到我有一个要发送到我的网络链接的字符串。当我对StringContent使用“ {\” text \“:\” Hello,World!\“}”时,代码可以正常运行,但是如果我尝试用output_message字符串替换它,它将无法正常工作。我正在尝试找出如何将output_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!\“}”

2 个答案:

答案 0 :(得分:1)

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

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

因此,对于您的问题,我们创建一个带有属性文本的匿名对象,并传递值Aoutput_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);
        }
    }
}

Examples

答案 1 :(得分:1)

据我所知,人们正在从JavaScriptSerializer转向Json.NET。甚至在文档here

中也建议使用

相应的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,您需要安装Newtonsoft.Json nuget软件包。