0

I'm learning on how to consume third-party API, (I don't have any control on how the data should be formatted).

I followed a tutorial online, an I'm stuck on this error I can't return a json. my data from API looks like this

{"status":1,"message":"","data":{"VacationLeave":11,"SickLeave":10,"EmergencyLeave":2,"HolidaySwap":1,"OldLeave":1}}

The error thats been thrown on compilation.

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[HttpClientTest.Models.LeaveModel]' 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<T>) 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.

my controller


public IActionResult Privacy()
        {
            
            //Fetch the JSON string from URL.
            List<LeaveModel> leaves = new List<LeaveModel>();
            string apiUrl = "http://xxx.xxx.xxx:83/GetLeaveBalance/2170";

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.GetAsync(apiUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                leaves = JsonConvert.DeserializeObject<List<LeaveModel>>(response.Content.ReadAsStringAsync().Result);
                
            }

            //Return the Deserialized JSON object.
            return Json(leaves);
        }
  • 4
    You seem to be missing the parent object that contains `status`, `message`, and `data`. Also `data` is an object itself, not an array, so you certainly shouldn't be involving any lists. – ProgrammingLlama Apr 19 '22 at 05:44
  • @DiplomacyNotWar so what should I do? I'm kinda lost on what's the solution – John Phillip Abello Apr 19 '22 at 06:28
  • Make C# classes that match your JSON. Where you have an object (`{....}`) in JSON, you have a class. Where you have an array (`[...]`) you have a collection (e.g. List) in C#. As it is, you have no arrays, so you just need two classes: the parent class with `status`, `message`, and `data`, and then the child class with `VacationLeave`, `SickLeave`, `EmergencyLeave`, `HolidaySwap`, and `OldLeave`. The parent class's `data` property will have the type of the child class. – ProgrammingLlama Apr 19 '22 at 06:32
  • It's really as simple as making your C# class structure and property names match the structure and property names of the JSON. You can't just make arrays where there aren't any in the JSON, or skip levels of the JSON. – ProgrammingLlama Apr 19 '22 at 06:33
  • @DiplomacyNotWar you mean I should make another model class for `status`,`message` and `data` and then `data` is a type of `List`? – John Phillip Abello Apr 25 '22 at 00:45
  • Yes, but data is singular (`LeaveModel` not `List`) – ProgrammingLlama Apr 25 '22 at 01:51

0 Answers0