我正在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."}

推荐答案

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.

Json相关问答推荐

当有嵌套数组而没有嵌套数组时,展平JSON

我可以使用JQ来缩小数组中的json对象的范围吗?

在Jenkins中使用ReadJSON读取json子元素

如何使用GoFr返回XML响应?

在 SQL 存储过程中使用参数 Select 值

Golang jsonrpc2 服务器在哪里监听?

使用 map_values Select 包含空格的字符串未按预期工作

jq - 将父键值提取为子元素旁边的逗号分隔值

使用 jq 将消息转换为数组

为什么解析的字典相等而腌制的字典不相等?

JQ 中的样本标准偏差

jq:用列表包装所有第一级原子值

缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter

一起使用 Argparse 和 Json

什么是类型和类型令牌?

如何通过 NSJSONSerialization 在 JSON 中包含空值?

将错误消息作为 JSON 对象发送

Jackson 没有使用 @JsonProperty 覆盖 Getter

如何向从文件中检索的 JSON 数据添加键值?

ASP.NET MVC 读取原始 JSON 发布数据