C# 通过HttpWebRequest在Json内传递符号

C# 通过HttpWebRequest在Json内传递符号,c#,json,httpwebrequest,symbols,C#,Json,Httpwebrequest,Symbols,我尝试使用下面的方法将消息以Json格式传递给MS Flow,但一旦传递了任何符号(如“),就会出现错误,因为符号被识别为代码 public static bool notification(string customer, string comment) { try { var httpWebRequest = (HttpWebRequest)WebRequest.Create("my msflow link goes here"

我尝试使用下面的方法将消息以Json格式传递给MS Flow,但一旦传递了任何符号(如“),就会出现错误,因为符号被识别为代码

public static bool notification(string customer, string comment)
    {
        try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("my msflow link goes here");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{ \"customer\":\"" + customer + "\",\"comment\":\"" + comment + "\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();

            }

            return true;
        }
        catch (Exception)
        {
            return false;
        }

    }
尝试在代码中使用序列化对象,如下所示:

string json = JsonConvert.SerializeObject(<your object>);
string json=JsonConvert.SerializeObject();

像这样手工创建JSON是个坏主意——正如您所发现的那样,它容易出现愚蠢的语法错误,以及某些字符的解释问题。相反,创建一个C#对象,然后使用类似的方法对其进行序列化。这样,序列化程序将处理数据中的任何特殊字符并转义/enco对它们进行适当的分类。