即使我尝试投射到具有匹配属性的对象,也无法投射类型为Newtonsoft.Json.Linq.JObject的对象 [英] Unable to cast object of type Newtonsoft.Json.Linq.JObject even though I am trying to cast to an object with matching properties

查看:211
本文介绍了即使我尝试投射到具有匹配属性的对象,也无法投射类型为Newtonsoft.Json.Linq.JObject的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS2017中使用ASP.NET Core 2.0.

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

我正在尝试反序列化在HttpResponseMessage中返回的一些JSON,但出现无法转换类型...的对象"异常.

I am trying to deserialize some JSON that is returned in an HttpResponseMessage but I am getting an "Unable to cast object of type..." exception.

这是失败的代码;

FilesUploadedListResponse fileUploadListResponse = new FilesUploadedListResponse();
string jsonResult = response.Content.ReadAsStringAsync().Result;
fileUploadListResponse = (FilesUploadedListResponse)JsonConvert.DeserializeObject(jsonResult);

最后一行是我收到异常消息的地方...

The last line is where I get the exception...

"Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'FilesUploadedListResponse'."

这是字符串jsonResult中的实际Json:

Here is the actual Json that is in the string jsonResult:

"{\"uploadedfiles\":[],\"totalResults\":0,\"pageNumber\":0,\"pageSize\":0}"

uploadedFiles数组在此结果中为空,因为还没有上传的文件,但是我认为将其清空不应该创建异常,不是吗?如果不为空,则其响应将类似于以下内容:

The uploadedFiles array is empty in this result, because there are no uploaded files yet, but I would think that having it empty shouldn't create an exception, should it? If it were not empty, it would have a response similar to this:

{
 "uploadedFiles": [
 {
 "id": 1,
 "createdTime": "July 10, 2017 02:02:25 PM",
 "filename": "20170710_14022507701.jpg",
 "sentTime": "July 10, 2017 02:05:11 PM",
 "fileSize": "124 KB"
 },
 {
 "id": 2,
 "createdTime": "June 05, 2017 09:39:25 AM",
 "filename": "20170605_093907701.jpg",
 "sentTime": "June 05, 2017 09:40:11 AM",
 "fileSize": "1 MB"
 }
],
 "totalResults": 2,
 "pageNumber": 0,
 "pageSize": 2
}

这是我的FileUploadListResponse课:

public class FilesUploadedListResponse
{
    public bool Success { get; set; }
    public string Reason { get; set; }
    public int StatusCode { get; set; }
    public List<UploadedFile> UploadedFiles { get; set; }
    public int TotalResults { get; set; }
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
}

这是我的UploadedFile课:

public class UploadedFile
{
    public int Id { get; set; }
    public DateTime CreatedTime { get; set; }
    public string Filename { get; set; }
    public DateTime? SentTime { get; set; }
    public string FileSize { get; set; }
}

我对JSON反序列化的理解是:

My understanding of the JSON deserialization is that:

  1. 在JSON字符串中的值与我要反序列化的类对象之间,元素的大小写无关紧要.

  1. The case of the elements doesn't matter between the values in the JSON string and the class object I am trying to deserialize to.

我要反序列化的类可以具有JSON字符串中提供的更多属性,只要考虑JSON字符串中的属性即可.

The class I am trying to deserialize to can have more properties that what is provided in the JSON string, just as long as the properties that are in the JSON string are accounted for.

一个空的子数组,例如UploadedFiles数组,在尝试反序列化为List<UploadedFile>

An empty sub array, such as the UploadedFiles array should not cause an error when trying to deserialize to the List<UploadedFile>

我确定这很简单,但我只是没有看到它.我在这里想念什么?

I am sure it is something simple, but I am just not seeing it. What am I missing here?

推荐答案

调用非通用方法 JsonConvert.DeserializeObject(jsonResult) ,您要求Json.NET将传入的JSON反序列化为自己选择的某种.Net类型 ,足以捕获传入的JSON.实际上选择的是 LINQ to JSON

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

这篇关于即使我尝试投射到具有匹配属性的对象,也无法投射类型为Newtonsoft.Json.Linq.JObject的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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