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

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

问题描述

我正在尝试反序列化从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");
}

但是它给出错误:无法将System.String强制转换或转换为App1.ApiResult App.Authentication = JsonConvert.DeserializeObject<ApiResult>(r);

App.Authentication:

public static ApiResult Authentication = new ApiResult();`

JSON字符串:

"\" {\\状态\\":\\"0 \\",\\消息\\":{\\"ID \\":5,\\名字\\": \\"John \\",\\姓氏\\":\\"Doe \\",\\电子邮件\":\\"testemail@gmail.com \",\\密码\\ :\\" testPass \\,\\" CreationDate \\:\\" 2016-10-26T15:01:08 \\,\\" RoleID \\:1,\\" doorCode \\ :9999}} \""

ApiResult类:

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

完整的错误消息:

{转换值时出错 \"{\"状态\:\" 0 \,\"消息\:{\" ID \:5,\"名字\:\"约翰\,\"姓氏:\\ Doe \,\" Email \:\" testemail@gmail.com \,\" Password \:\" testPass \,\" CreationDate \:\" 2016-10-26T15:01:08 \ ,\" RoleID \:1,\" doorCode \:9999}} \" 键入"App1.ApiResult".路径",第1行,位置232.}

解决方案

似乎收到的json已被序列化两次-首先从ApiResultstring,然后再次到string:

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

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

尝试将数据反序列化为字符串,然后然后将其反序列化为ApiResult,以确保是这种情况-如果是这样,则需要更改服务器代码. /p>

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

解决方案

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.

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

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