-3

Hope everybody is having a good day. I am using an API to read from a DB and then testing this API with Postman.

I have a c# class that looks like this:

{

    public class ResultValidSubscriptions
    {
        int ID;
        string Integration;
        string Domain;
        int Month;
        int Year;
        int UserCount;
        string AppVersion;
        bool IsCorrect;

        public ResultValidSubscriptions(int iD, string integration, string domain, int month, int year, int userCount, string appVersion, bool isCorrect)
        {
            ID = iD;
            Integration = integration;
            Domain = domain;
            Month = month;
            Year = year;
            UserCount = userCount;
            AppVersion = appVersion;
            IsCorrect = isCorrect;
        }
    }
}

So I retrieve a list of these objects from the database and adding them to a list. I then use this method to return it to the caller(postman in this case).

  [HttpGet]
        public IHttpActionResult GetAllValidSubscriptions(int TargetYear, int TargetMonth)
        {
            try
            {
                arrayListSubscriptions = service.getAllValidSubscriptions(TargetYear, TargetMonth);
                string json = JsonConvert.SerializeObject(arrayListSubscriptions);
                var deserialized = JsonConvert.DeserializeObject<List<ResultValidSubscriptions>>(json);

                return Ok(deserialized);
            }
          

When I insert breakpoints I can see that arrayListSubscriptions contains the correct items. 5x ResultValidSubscriptions in this case. As you can see below the empty array also contains 5 items.

The problem is when I return either arrayListSubscriptions , Json, or deserialized all I get is an empty array.

enter image description here

When I change

return Ok(deserialized);

to


return Ok("hi");

It works fine and I see hi in postman. So my arraylist seems to be full, my method seems to be working. But somehow somewhere the array is getting cleared? I think the conversion to JSON is doing this, but I thought this would be solved by deserializing it with the class name.

Anybody that can help me with this? Much appreciated.

2
  • 2
    All your fields are private. Commented May 30, 2022 at 9:16
  • Already thought it had to be something stupid on my side. Thanks for the help! Solved. Commented May 30, 2022 at 9:20

1 Answer 1

2

Problem was the fields in my object class were private.

Changed to:


 public class ResultValidSubscriptions
    {
        public int ID;
        public string Integration;
        public string Domain;
        public int Month;
        public int Year;
        public int UserCount;
        public string AppVersion;
        public bool IsCorrect;

        public ResultValidSubscriptions(int iD, string integration, string domain, int month, int year, int userCount, string appVersion, bool isCorrect)
        {
            ID = iD;
            Integration = integration;
            Domain = domain;
            Month = month;
            Year = year;
            UserCount = userCount;
            AppVersion = appVersion;
            IsCorrect = isCorrect;
        }
    }

And it works! Sorry for the stupid oversight and thanks for the help!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.