gpt4 book ai didi

c# - RestSharp 显示无法创建接口(interface)实例的错误必须手动反序列化

转载 作者:太空狗 更新时间:2023-10-29 18:15:53 24 4
gpt4 key购买 nike

我有 RestSharp(类似于 HttpClient)调用并从 Web Api 方法返回数据

我收到此错误 {"Cannot create an instance of an interface."

我的代码是这样的:

public List<Interest> GetInterests()
{
var request = new RestRequest(apiPath + "GetInterests", Method.GET) { RequestFormat = DataFormat.Json };
var response = Client.Execute<List<Interest>>(request);
return response.Data;
}

我的response.Content如下所示。

现在的问题似乎是 DATA 在响应对象的 Content 属性中作为 JSON 存在。我可以从那里手动反序列化它。但是之前是自动将它反序列化到 Data 属性中,我不确定现在发生了什么。

如果我手动反序列化 json,它会起作用 - 但为什么我需要这样做?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 

响应:

"StatusCode: OK, Content-Type: application/json; charset=utf-8, Content-Length: 5597)"

返回 JSON 代码

[
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":1,
"InterestName":"Nursing",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":2,
"InterestName":"Creating/Supporting Networks of Community Partners",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":3,
"InterestName":"Epidemiology",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":4,
"InterestName":"Maternal",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":5,
"InterestName":"Healthcare for the Homeless",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":6,
"InterestName":"HIV/AIDS, STD",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":7,
"InterestName":"Nutrition",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":8,
"InterestName":"Physical Activity",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":9,
"InterestName":"Oral Health",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":10,
"InterestName":"Public Health Communication",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":11,
"InterestName":"Emergency Preparedness and Response",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":12,
"InterestName":"Public Health Policy",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":13,
"InterestName":"Tuberculosis",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":14,
"InterestName":"Teen Pregnancy Prevention",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":15,
"InterestName":"Vital Registration",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":16,
"InterestName":"Chronic Disease",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":17,
"InterestName":"Finance & Management",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":18,
"InterestName":"Quality Improvement",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":19,
"InterestName":"Healthy Community Design",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":20,
"InterestName":"Behavioral Health and Healthcare Integration",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":21,
"InterestName":"Rx Drug Misuse and Abuse",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":22,
"InterestName":"Injury Prevention",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":23,
"InterestName":"School Health",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":24,
"InterestName":"Worksite Wellness",
"AddDate":"2016-07-01T17:03:21.61",
"ModDate":"2016-07-01T17:03:21.64",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
},
{
"InternInterests":[

],
"OfficeInterests":[

],
"InterestId":25,
"InterestName":"Access to Healthy Foods",
"AddDate":"2016-08-03T08:10:45.4",
"ModDate":"2016-08-03T08:10:45.4",
"AddUser":"System",
"ModUser":"System",
"IsDeleted":false
}
]

如果我手动反序列化 json,它会起作用 - 但为什么我需要这样做?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 

最佳答案

我遇到了同样的问题。在我的例子中,我创建了一个自定义对象(在你的例子中是 Interest 对象),它有一个 ICollection 对象列表。因此,我删除了该 ICollection 并将其作为对象列表。检查 Interest 是否有任何接口(interface)相关的属性,并尝试将其制作为具体的对象属性。

检查 InternInterests 和 OfficeInterests 属性,它可能正在使用任何类型的接口(interface)集合。

关于c# - RestSharp 显示无法创建接口(interface)实例的错误必须手动反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com