我正在try 反序列化从Web API接收的JSON字符串
try
{
string r = await App.client.GetUser();
App.Authentication = JsonConvert.DeserializeObject<ApiResult>(r);
await DisplayAlert("TEST", App.Authentication.ToString(), "OK");
Application.Current.MainPage = new Schedule();
}
catch (Exception p)
{
await DisplayAlert("Getting Authentication failed", p.ToString(), "TEST");
}
However it gives the error: Could not Cast or Convert System.String to App1.ApiResult
App.Authentication = JsonConvert.DeserializeObject<ApiResult>(r);
应用程序.身份验证:
public static ApiResult Authentication = new ApiResult();`
JSON字符串:
"\\"状态\\":\\"0\",\\"消息\\":{\\"ID\":5,\\"名字\\":\\"约翰\\",\\"姓氏\\":\\"Doe\",\\"邮箱\\":\"testemail@gmail.com\\",\"密码\\":\"测试通行证\\",\"创建日期\\":\"2016-10-26T15:01:08\",\"RoleID\":1,\"门代码\\":9999}}"
ApiResult类:
public class ApiResult
{
public string status { get; set; }
public Account message { get; set; }
}
Account Class:
public class Account
{
public string status { get; set; }
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime CreationDate { get; set; }
public int RoleID { get; set; }
public int doorCode { get; set; }
}
The full error message:
{"转换值时出错 \"{\"status\":\"0\",\"message\":{\"ID\":5,\"FirstName\":\"John\",\"LastName\":\"Doe\",\"email\":\"testemail@gmail.com\",\"password\":\"testPass\",\"CreationDate\":\"2016-10-26T15:01:08\",\"RoleID 若要键入"App1.ApiResult",请执行以下操作.路径‘’,行1,位置232."}