当我进行 API 调用时,我得到以下响应
{
"AdPlanId": 1,
"PlanName": "SWIFT Test plan",
"Description": "Descritpion for Test plan",
"NoVideosToPlay": 2,
"NoBannersToShow": 3,
"MinTimeBetweenAds": 5,
"MaxTimeBetwenAds": 15,
"MinDataConsumption": 25,
"MaxDataConsumption": 100,
"Created": "2017-01-24T16:35:13.042211",
"LastModified": "2017-01-24T16:35:13.042712",
"MarkedForDeletion": null,
"MarkedDeletedDate": null
}
然后我尝试使用 C# 代码反序列化
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
return new DBResult<T>(true, JsonConvert.DeserializeObject<T>(content));
}
我在此行中收到"无法将空对象转换为值类型"异常 return new DBResult(true, JsonConvert.DeserializeObject(content((;请问我做错了什么?
我认为您应该尝试为您接收的对象类型创建一个代理类并编写它而不是.它大多无法将其转换为T型。
创建一个代理类,如下所示,然后将
class ClassName{
string AdPlanId {get; set;}
string PlanName {get; set;}
string Description {get; set;}
string NoVideosToPlay {get; set;}
string NoBannersToShow {get; set;}
string MinTimeBetweenAds {get; set;}
string MaxTimeBetwenAds {get; set;}
string MinDataConsumption {get; set;}
string MaxDataConsumption {get; set;}
string Created {get; set;}
string LastModified {get; set;}
string MarkedForDeletion {get; set;}
string MarkedDeletedDate {get; set;}
}
然后执行以下操作:
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
return new DBResult<ClassName>(true, JsonConvert.DeserializeObject<ClassName>(content));
}