C# Could not Cast or Convert System.String to Class object

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

I am trying to deserialize a JSON string received 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 the error: Could not Cast 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\\",\\"Email\\":\\"testemail@gmail.com\\",\\"Password\\":\\"testPass\\",\\"CreationDate\\":\\"2016-10-26T15:01:08\\",\\"RoleID\\":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; } } 

The full error message:

{"Error converting value \"{\"status\":\"0\",\"message\":{\"ID\":5,\"FirstName\":\"John\",\"LastName\":\"Doe\",\"Email\":\"testemail@gmail.com\",\"Password\":\"testPass\",\"CreationDate\":\"2016-10-26T15:01:08\",\"RoleID\":1,\"doorCode\":9999}}\" to type 'App1.ApiResult'. Path '', line 1, position 232."}

回答1:

It appears that the json you receive has been serialized twice - first from ApiResult to string, then to string again:

"\"{\\"status\\":\\"0\\",\\"message\\":... 

The first double-quote might be added by your debugger, but the second (the escaped \" one) really appears to be part of the data you're processing. The error message also makes sense this way, it deserializes a string and then attempts to cast it to an ApiResult.

Try deserializing the data as a string and then deserializing its result to an ApiResult, to be sure this is the case - and if so, the server code will need to be changed.



回答2:

your data model,

   var r = new ApiResult             {                 status = "0",                 message = new Account()                 {                     status = "0",                     CreationDate = DateTime.Now,                     Email = "foo@foo.com",                     FirstName = "Trump",                     ID = 1,                     LastName = "Fck",                     Password = "111",                     RoleID = 1,                     doorCode = 222                 }             };   var jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(r);  var apiObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult>(jsonResult); 

jsonResult:

to problem look here App.client.GetUser();



回答3:

Try using App.Authentication = JObject.Parse(request.Content.ReadAsStringAsync().Result);



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!