我想将json字符串发送到服务器,首先我写这个json:
string json = "{\"registration_ids\":[\"" + regId + "\"]}";
服务器起床json成功,但当我改变它:
string json = "{\"registration_ids\":[\"" + regId + "\"] , \"data\":[\"" + ns + "\"]}";
现在不发送那个json,我得到这个错误:
The remote server returned an error: (400) Bad Request.
会发生什么?我该如何解决?
string ns = "ali";
string regId = "APA91bGUgyf8rIVBIdRyc9JUZDWfBTBkflRB2xKe5u9zfd6xP8PYR0yWHXoA7_ev51bAuGHYLjzYuC-TwbzmfO35synRkjf5UoRTTMMYmKgi7qRgb6HPbIv9JFf_xLNd1IKKwriPUl3vCdGYtPMJj42GJir-";
答案 0 :(得分:1)
因为您可能创建了无效的JSON。不要写'&34;手写'"你的JSON就是这样。相反,请使用为您执行此操作的库,例如Json.NET:
public class X
{
[JsonProperty("registration_ids")]
public List<string> RegistrationIds { get; set; }
[JsonProperty("data")]
public List<string> Data { get; set; }
}
并像这样使用它:
static void Main(string[] args)
{
var x = new X
{
RegistrationIds = new List<string> { "1", "2" },
Data = new List<string> { "hello" }
};
Console.WriteLine(JsonConvert.SerializeObject(x));
}
将屈服:
{"registration_ids":["1","2"],"data":["hello"]}