从响应的IList创建IList时出现错误异常,错误是:
{“值不能为空。\ r \ n参数名称:集合”}
json:
{ "status": "success", "message": null, "response": [ { "Id": 1, "CandidateId": "123456", "FirstName": "AAA", "LastName": "BBB", "Gender": "M", "DateOfBirth": "1984-01-01T00:00:00" }, { "Id": 2, "CandidateId": "12458", "FirstName": "CCC", "LastName": "DDD", "Gender": "M", "DateOfBirth": "1980-05-15T00:00:00" }], "Timestamp": "2019-05-12T10:07:15.6733692+03:00"}
响应类别为:
public class ResponseMessage
{
public string status { get; set; }
public string message { get; set; }
public IList response { get; set; }
public DateTime Timestamp { get; set; }
}
我的代码是:
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
data.Wait();
ResponseMessage responseMessage = new JavaScriptSerializer().Deserialize<ResponseMessage>(data.Result);
if (responseMessage.status == "success")
{
IList<Candidate> _candidates = new List<Candidate>(responseMessage.response as IList<Candidate>);
ViewData["candidate"] = _candidates;
}
}
如果您注意到响应类中的IList是通用IList作为主要的api类,以避免重复的ResponseMessage。 _candidate引发的错误