C# 为什么我的字符串抛出错误?

C# 为什么我的字符串抛出错误?,c#,urbanairship.com,C#,Urbanairship.com,我想针对城市飞艇api的字符串类型“application/json”。你们能帮我看看我的绳子和结构有什么问题吗 string postData = "{"audience":"all","device_types":["android"],"notification":{"alert":"This is a broadcast."}}"; 我确实尝试过在下面添加反斜杠“\”示例: string postData = "{\"audience\":\"all\",\"device_types\

我想针对城市飞艇api的字符串类型“application/json”。你们能帮我看看我的绳子和结构有什么问题吗

string postData = "{"audience":"all","device_types":["android"],"notification":{"alert":"This is a broadcast."}}";
我确实尝试过在下面添加反斜杠“\”示例:

string postData = "{\"audience\":\"all\",\"device_types\":[\"android\"],\"notification\":{\"alert\":\"This is a broadcast.\"}}";
以下是我的完整代码:

 // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/");
            // Set the Method property of the request to POST.
            request.Method = "POST";

            // Create POST data and convert it to a byte array. 

            string postData = "{'audience':'all','device_types':'['android']','notification': {'alert':'This is a broadcast.'}";




            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/json";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            //Do a http basic authentication somehow
            string username = "Zx5EPSG7Qhu-BvYtz0laTg";
            string password = "sIaj2CjASlm27pimHqOfhA";
            string usernamePassword = username + ":" + password;
            CredentialCache mycache = new CredentialCache();
            mycache.Add(new Uri("https://go.urbanairship.com/api/push/"), "Basic", new NetworkCredential(username, password));
            request.Credentials = mycache;
            request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
我得到以下错误:

400 Bad Request – The request body was invalid, either due to malformed JSON or a data validation error. See the response body for more detail.

您在第一个值中键入了小字符,在
“all”
后面加了一个
符号:

将来,您可以对此类情况使用在线验证器,例如。

这应该可以:

{
     "audience" : "all",
     "device_types" : ["android"],
     "notification" : {
         "alert" : "This is a broadcast."
     }
 }

响应主体中有什么?您正在手动构建JSON字符串。那是个坏主意。相反,请使用库。
“观众”:“所有”
尝试以下操作:
string postData=“{'academy':'all','device_types':'['android'],'notification':{'alert':'this a broadcast'.}”;
很抱歉,“…我改为single”的输入错误仍然相同
{
     "audience" : "all",
     "device_types" : ["android"],
     "notification" : {
         "alert" : "This is a broadcast."
     }
 }