Home > OS >  Consuming REST API returning multiple objects
Consuming REST API returning multiple objects

Time:03-23

I am writing code to use a REST API. I have no access to the API code itself. The API for some reason is written as POST although, to my point of view, it is a pure GET. Anyway, I cannot change it and have to use it as-is. The POST gets one parameter - GUID token - and returns two objects: action result, containing error code and error description, and list of available domains - domain code and domain name. Pretty simple, heh? Here is the JSON (via the Insomnia):

{
    "token": "BC3F77C6-4CA4-4CC5-AB68-2D563321CD0A"
}

Here is what is returned

{
  "actResult": {
    "ResultCode": 0,
    "ErrorDescription": ""
  },
  "domains": {
    "lstDomains": [
      {
        "nDomainID": 1,
        "DomainName": "DEV"
      },
      {
        "nDomainID": 2,
        "DomainName": "SALES"
      },
      {
        "nDomainID": 3,
        "DomainName": "SUPPORT"
      },
      {
        "nDomainID": 4,
        "DomainName": "PAYROLL"
      }
    ]
  }
}

Now, the code

string sBaseUrl="https://Whatever/API/"
private void PerformDomainsList()
{
    client.BaseAddress = new Uri(sBaseURL   "getDomains");
    BaseEntity tok = new BaseEntity
    {
        token = new Guid("BC3F77C6-4CA4-4CC5-AB68-2D563321CD0A")
    };
    var postTaskG = client.PostAsJsonAsync<BaseEntity>("", tok);
    postTaskG.Wait();
    var result = postTaskG.Result;
    if (result.IsSuccessStatusCode)
    {
        var pRslt = result.Content.ReadAsAsync<DomainsResult>();
        pRslt.Wait();
               
        DomainsResult z = pRslt.Result;
    }
}

Class definitions:

    public class DomainsResult
    {
        public ActResult actResult;
        public List<Domains> domains;
    }
    public class ActResult
    {
        public int ResultCode { get; set; }
        public string ErrorDescription { get; set; }
    }
    public class Domains
    {
        public int nDomainID { get; set; }
        public string DomainName { get; set; }
    }

Everything works fine with another API, which returns single ActResult object. But with this one it fails with an error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1....

Searched a lot, but the proposal is to change the API itself, which is not an option...

CodePudding user response:

Found it. As the error said, Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List ... And this is indeed the case. As it can be seen in Json, the reply is actually two objects - actResult and domains. The class I created for reply was different - actResult and list of domains. Wrapping List of Domains solved the problem:

    public class LstDomains
    {
        public List<Domains> lstDomains;
    }
    public class DomainsResult
    {
        public DanaRequestResult danaResult;
        public LstDomains domains;
    }
  •  Tags:  
  • rest
  • Related