Unable to cast object of type Newtonsoft.Json.Linq.JObject even though I am trying to cast to an object with matching properties

前端 未结 1 375
我寻月下人不归
我寻月下人不归 2020-12-21 20:34

I am working with ASP.NET Core 2.0 in VS2017.

I am trying to deserialize some JSON that is returned in an HttpResponseMessage but I am getting an \"Unab

相关标签:
1条回答
  • 2020-12-21 20:35

    When you call the non-generic method JsonConvert.DeserializeObject(jsonResult), you are asking Json.NET to deserialize the incoming JSON into some .Net type of its own choosing that is sufficient to capture the incoming JSON. What it in fact chooses is a LINQ to JSON JObject. Since this type is not implicitly or explicitly convertible to your FilesUploadedListResponse type, you get the exception you see.

    Since want to deserialize to a specific, known type, you should instead call the generic method JsonConvert.DeserializeObject<FilesUploadedListResponse>(jso‌​nResult) which Deserializes the JSON to the specified .NET type like so:

    string jsonResult = response.Content.ReadAsStringAsync().Result;
    var fileUploadListResponse = JsonConvert.DeserializeObject<FilesUploadedListResponse>(jsonResult);
    
    0 讨论(0)
提交回复
热议问题