简体   繁体   English

为什么在从Rest服务反序列化此json对象时出现此转换错误?

[英]Why am I getting this casting error when deserializing this json object from a Rest Service?

I am trying to deserialize a json response that i am getting from a rest service (using RestSharp) but i keep getting this error: 我试图反序列化我从休息服务(使用RestSharp)获得的json响应,但我不断收到此错误:

Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'. 无法将类型为'RestSharp.JsonArray'的对象强制转换为'System.Collections.Generic.IDictionary`2 [System.String,System.Object]'。

The response looks like this: 响应如下:

“message” : “Successfully authenticated.”,
“total” : null,
“data” : [ {
  “token” : “XYZ”,
  “user” : {
    “password” : “ABC”,
    “username” : “User”
   }
}],
“success” : true

and I have created the following C# objects: 我创建了以下C#对象:

internal class AuthenticationResponse
{
    public string message { get; set; }
    public string total { get; set; }
    public AuthenticationResponseData data { get; set; }
    public bool success {get;set;}
}

internal class AuthenticationResponseData
{
    public string token { get; set; }
    public AuthenticationUser user {get;set;}
}

internal class AuthenticationUser
{
      public string username {get;set;}
       public string password {get;set;}
}

but i keep getting the following error: 但我不断收到以下错误:

Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'. 无法将类型为'RestSharp.JsonArray'的对象强制转换为'System.Collections.Generic.IDictionary`2 [System.String,System.Object]'。

Because in the JSON the data value is an array. 因为在JSON中data值是一个数组。 You need to change your AuthenticationResponse class to represent AuthenticationResponseData as such. 您需要更改AuthenticationResponse类以表示AuthenticationResponseData。

internal class AuthenticationResponse
{
    public string message { get; set; }
    public string total { get; set; }
    public List<AuthenticationResponseData> data { get; set; }
    public bool success {get;set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到 JSON 反序列化错误和 REST API? - Why am I getting a JSON deserializing error with a REST API? 反序列化 Json 对象时出现 Newtonsoft.Json.JsonSerializationException 错误 - Getting Newtonsoft.Json.JsonSerializationException error when deserializing a Json object 在REST服务中反序列化JSON - Deserializing JSON in REST Service 无法从REST服务反序列化Json - Trouble deserializing Json from REST service 将 JSON 反序列化为对象时出错 - Error when deserializing JSON to Object 为什么我在使用这个 JSON_OBJECT 时遇到错误 - Why am i getting error with this JSON_OBJECT usage 在 vb 中反序列化 JSON 时出错,为什么我会收到异常详细信息:System.ArgumentException:传入的无效数组,“,”预期? - Error Deserializing JSON in vb, Why am I getting Exception Details: System.ArgumentException: Invalid array passed in, ',' expected? 当我尝试从这个 JSON 文件中获取信息时,为什么会出现语法错误? - Why am I getting a syntax error when I try to take info from this JSON file? 反序列化 JSON 时转换失败 - Casting fails when deserializing JSON 为什么在读取 .json 文件时会出现此错误? - why am i getting this error when reading a .json file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM