无法将 System.String 强制转换或转换为 Class 对象 [英] Could not Cast or Convert System.String to Class object

查看:125
本文介绍了无法将 System.String 强制转换或转换为 Class 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化从 Web API 收到的 JSON 字符串

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");
}

但是它给出了错误:Could not Cast or Convert System.String to App1.ApiResultApp.Authentication = JsonConvert.DeserializeObject(r);

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 字符串:

"\"{\\"status\\":\\"0\\",\\"message\\":{\\"ID\\":5,\\"FirstName\\":\\"John\\",\\"LastName\\":\\"Doe\\",\\"Email\\":\\"testemail@gmail.com\\",\\"密码\\":\\"testPass\\",\\"CreationDate\\":\\"2016-10-26T15:01:08\\",\\"RoleID\\":1,\\"doorCode\\":9999}}\""

"\"{\\"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 类:

ApiResult Class:

public class ApiResult
{
    public string status { get; set; }
    public Account message { get; set; }
}

帐户类别:

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; }
}

完整的错误信息:

{"转换值时出错\"{\"status\":\"0\",\"message\":{\"ID\":5,\"FirstName\":\"John\",\"LastName\":\"Doe\",\"Email\":\"testemail@gmail.com\",\"密码\":\"testPass\",\"CreationDate\":\"2016-10-26T15:01:08\",\"RoleID\":1,\"doorCode\":9999}}\"输入App1.ApiResult".路径 '',第 1 行,位置 232."}

{"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."}

推荐答案

看来你收到的 json 已经被序列化了两次 - 首先从 ApiResultstring,然后再次string:

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

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

您的调试器可能会添加第一个双引号,但第二个(转义的 \" )似乎确实是您正在处理的数据的一部分.错误消息也使通过这种方式,它会反序列化一个 string,然后尝试将其转换为 ApiResult.

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.

尝试将数据反序列化为字符串,然后然后将其结果反序列化为 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.

这篇关于无法将 System.String 强制转换或转换为 Class 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆