简体   繁体   English

无法将'Newtonsoft.Json.Linq.JObject'转换为实际类型

[英]Can't cast 'Newtonsoft.Json.Linq.JObject' to actual type

I have the following code for getting a response from my server. 我有以下代码,可从服务器获取响应。 I can deserialize the response to my ApiResponse object type, but I can't cast an inner object to it's real type. 我可以反序列化对ApiResponse对象类型的响应,但不能将内部对象转换为它的实型。

Here is where I am getting the response from the server: 这是我从服务器获取响应的地方:

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    GlobalTestVariables.Response = JsonConvert.DeserializeObject<ApiResponse>(result);
    var a = GlobalTestVariables.Response.Result as TokenModel; // a = null
}

The value of var result = streamReader.ReadToEnd(); var result = streamReader.ReadToEnd();的值var result = streamReader.ReadToEnd(); is as follows: 如下:

var result = "{\"IsSuccess\":true,\"Message\":\"Login Successful\",\"Result\":{\"id_token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6InBsaW10ZXN0aW5nIiwibmJmIjoxNTE1MDc5NzMyLCJleHAiOjE1MTUwODA5MzIsImlhdCI6MTUxNTA3OTczMn0.wcr5zndSwdrDL7huea_oWpAl8ohL0GL3NZOmc_VBduc\"},\"Status\":200}"

This is what's being returned: 这是返回的内容:

return new ApiResponse(true, HttpStatusCode.OK, "Login Successful", new TokenModel(JwtManager.GenerateToken(user.Username)));

Where ApiResponse is: 其中ApiResponse是:

[DataContract]
public class ApiResponse
{
    [DataMember]
    public bool IsSuccess { get; set; }
    [DataMember]
    public String Message { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public Object Result { get; set; }
    [DataMember]
    public int Status { get; set; }

    public ApiResponse() { }

    public ApiResponse(bool isSuccess, HttpStatusCode statusCode, string message = null, object result = null)
    {
        IsSuccess = isSuccess;
        Status = (int)statusCode;
        Result = result;
        Message = message;
    }
}

And TokenModel is: TokenModel是:

public class TokenModel
{
    public string id_token { get; set; }
    public TokenModel(string token)
    {
        this.id_token = token;
    }
    public TokenModel() { }
}

Does anybody know how to deserialize it to it's correct type? 有人知道如何将其反序列化为正确的类型吗?

There are two options: 有两种选择:

  1. You change the type of ApiResponse.Result to TokenModel . 您将ApiResponse.Result的类型ApiResponse.ResultTokenModel The deserializer then expects a TokenModel and will deserialize it appropriately. 然后,反序列化程序需要一个TokenModel并将对其进行反序列化。

  2. If the type of the property Result is dynamic, you need to include type information in the serialized JSON string. 如果属性Result的类型是动态的,则需要在序列化的JSON字符串中包括类型信息。 For that, have a look at the TypeNameHandling setting. 为此,请查看TypeNameHandling设置。

As long as Result always has id_token in the JSON, it's as simple as changing Result to TokenModel . 只要Result在JSON中始终具有id_token ,就像将Result更改为TokenModel一样简单。

    [DataContract]
public class ApiResponse
{
    [DataMember]
    public bool IsSuccess { get; set; }
    [DataMember]
    public String Message { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public TokenModel Result { get; set; }
    [DataMember]
    public int Status { get; set; }

    public ApiResponse() { }

    public ApiResponse(bool isSuccess, HttpStatusCode statusCode, string message = null, TokenModel result = null)
    {
        IsSuccess = isSuccess;
        Status = (int)statusCode;
        Result = result;
        Message = message;
    }
}

暂无
暂无

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

相关问题 Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray' - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray' 无法将类型为“Newtonsoft.Json.Linq.JObject”的对象强制转换为类型<MyClass> - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type <MyClass> 无法将“Newtonsoft.Json.Linq.JObject”类型的 object 转换为类型“System.Collections.Generic.IDictionary” - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.IDictionary 无法将类型为“Newtonsoft.Json.Linq.JObject”的对象强制转换为“System.Runtime.Serialization.ISafeSerializationData” - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Runtime.Serialization.ISafeSerializationData' &#39;Newtonsoft.Json.Linq.JObject&#39; 类型的对象无法转换为类型 - Object of type 'Newtonsoft.Json.Linq.JObject' cannot be converted to type 无法将类型“Newtonsoft.Json.Linq.JObject”转换为复杂类型 - Cannot convert type 'Newtonsoft.Json.Linq.JObject' to Complex Type 无法隐式转换类型&#39;Newtonsoft.Json.Linq.JObject&#39; - Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' 导入的类型“ Newtonsoft.Json.Linq.JObject”被多次定义 - The imported type `Newtonsoft.Json.Linq.JObject' is defined multiple times 不支持集合类型“Newtonsoft.Json.Linq.JObject” - he collection type 'Newtonsoft.Json.Linq.JObject' is not supported 无法将 Newtonsoft.Json.Linq.JObject 转换为 Newtonsoft.Json.Z7FB58B61118E5084CA - Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM