I am trying to deserialize a JSON string retrieved from a web API
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 an error: Failed to enable or convert System.String to App1.ApiResult App.Authentication = JsonConvert.DeserializeObject<ApiResult>(r);
App.Authentication:
public static ApiResult Authentication = new ApiResult();`
JSON string:
"\" {\\ "status \\": \\ "0 \\", \\ "\\" message: {\\ "\\" ID: 5, \\ "\\" FirstName: \\ " John \\ ", \\" LastName \\ "\\" \\ Doe ", \\" \\ e-mail ": \\" testemail@gmail.com \\ ", \\" \\ Password ": \ \ "testPass \\" \\ "CreationDate \\": \\ "2016-10-26T15: 01: 08 \\", \\ "\\ Role ID": 1, \\ "\\ doorCode": 9999 }} \ ""
ApiResult Class:
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; } }
Full error message:
{"Error conversion value \" {\ "Status \": \ "0 \" \ "message \": {\ "ID \": 5, \ "FirstName \": \ "John \", \ "LastName \ ": \" Doe \ "\" Send \ ": \" testemail@gmail.com \ ", \" Password \ ": \" testPass \ ", \" CreationDate \ ": \" 2016-10-26T15: 01: 08 \ "\" Role ID \ ": 1, \" doorCode \ ": 9999}} \" to enter "App1.ApiResult". Path '', line 1, position 232. " }