I need to consume an external Web API so I can Search and View the data in a GRID format.
So far here is my code:
***Model***
public class AlertMessage
{
public string Title { get; set; }
public string Message { get; set; }
public string Severity { get; set; }
}
public class ItemLine
{
public string Description { get; set; }
public int BilledAmount { get; set; }
public int PaidAmount { get; set; }
}
public class CitationDetail
{
public string CitationNumber { get; set; }
public string DefendantName { get; set; }
public string DateofBirth { get; set; }
public string VehicleTagNumber { get; set; }
public string CaseType { get; set; }
public string CaseStatus { get; set; }
public string AppearanceDate { get; set; }
public bool IsPayable { get; set; }
public int FineSuspended { get; set; }
public int FineServed { get; set; }
public List<AlertMessage> AlertMessages { get; set; }
public List<ItemLine> ItemLines { get; set; }
}
public class Root
{
public List<CitationDetail> CitationDetails { get; set; }
public int CitationCount { get; set; }
public bool SuccessfulSearch { get; set; }
public string ErrorMessage { get; set; }
}
**Controller**
string Baseurl = "http://10.241.2.68:8109/";
public async Task<ActionResult> Index()
{
List<CitationDetail> CitInfo = new List<CitationDetail>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage Res = await client.GetAsync("api/Juris/Citation/7082^");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the citation list
CitInfo = JsonConvert.DeserializeObject<List<CitationDetail>>(EmpResponse);
}
//returning the citation list to view
return View(CitInfo);
}
}
I'm currently getting this error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CPA.Models.CitationDetail]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'CitationDetails', line 1, position 19.
***JSON*****
{
"CitationDetails": [
{
"CitationNumber": "00000708244",
"DefendantName": ", ",
"DateofBirth": "",
"VehicleTagNumber": "",
"CaseType": "CITATION",
"CaseStatus": "",
"AppearanceDate": "",
"IsPayable": true,
"FineSuspended": 0,
"FineServed": 0,
"AlertMessages": [],
"ItemLines": [
{
"Description": "Fine Amount",
"BilledAmount": 0,
"PaidAmount": 0
}
]
}
],
"CitationCount": 1,
"SuccessfulSearch": true,
"ErrorMessage": ""
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…