Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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": ""

}

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
128 views
Welcome To Ask or Share your Answers For Others

1 Answer

I can' t see your output json file, so my best guess will be replacing

   var EmpResponse = Res.Content.ReadAsStringAsync().Result;
 CitInfo = JsonConvert.DeserializeObject<List<CitationDetail>>(EmpResponse);

by

 var empResponse = await Res.Content.ReadAsStringAsync();
var empResponseObj = JsonConvert.DeserializeObject<Root>(empResponse);
 .......

 return View(empResponseObj.CitationDetails);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...