-1

I am making calls to an API which returns results in JSON format at 5 results per page. I'm making 5 calls to get all the results. I have a custom type created and can deserialize the first JSON page without issue. I need to then add the other pages to the same object so that I can later loop though the object to use as a key/value lookup. I usually code PHP and would simply use array_push to combine arrays, and then later loop through the array to find the id / display_number I'm looking for.

The JSON

All of the JSON is in the below format.

{
    "meta": {
        "paging": {
            "next": "https://URL/api/v4/companies.json?limit=5&page_token=t324vbf8673"
        },
        "records": 25
    },
    "data": [
        {
            "id": 1096720,
            "display_number": "Company:00001"
        },
        {
            "id": 1096723,
            "display_number": "Company:00002"
        },
        {
            "id": 1096726,
            "display_number": "Company:00003"
        },
        {
            "id": 1096729,
            "display_number": "Company:00004"
        },
        {
            "id": 1096732,
            "display_number": "Company:00005"
        },
}

The Code

I am using this to deserialize into my custom type object called Companies

Companies Company = JsonConvert.DeserializeObject<Companies>(response.Content);

This part is fine, but obviously in a loop, it will overwrite the previous object, rather than combine into a single one.

The Question

How can I combine the last page with the previous page (for a total of 5 pages)?

EDIT

The API Call:

public Companies GetCompanies(int records, string pageurl="")
    {
        var client = new RestClient();

        if(pageurl != "")
        {
            client = new RestClient(pageurl);
        }
        else
        {
            client = new RestClient(this.BaseURI + "companies.json");
        }
        var request = new RestRequest(Method.GET);
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        request.AddHeader("Authorization", this.TokenType + " " + this.AccessToken);
        request.AddParameter("limit", records);

        IRestResponse response = client.Execute(request);

        var content = response.Content;

        Companies Company = JsonConvert.DeserializeObject<Companies>(response.Content);
        return Company;
    }

The Class:

namespace Uploader
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Companies
    {
        [JsonProperty("meta")]
        public Meta Meta { get; set; }

        [JsonProperty("data")]
        public Datum[] Data { get; set; }
    }

    public partial class Datum
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("display_number")]
        public string DisplayNumber { get; set; }
    }

    public partial class Meta
    {
        [JsonProperty("paging")]
        public Paging Paging { get; set; }

        [JsonProperty("records")]
        public long Records { get; set; }
    }

    public partial class Paging
    {
        [JsonProperty("next")]
        public Uri Next { get; set; }
    }
}

How I'm using the class:

Companies GetCompanies = Clio.GetCompanies(5);
do
{
    GetCompanies = Clio.GetCompanies(5, GetCompanies.Meta.Paging.Next.ToString());
} while (GetCompanies.Meta.Paging.Next != null);
user2924019
  • 1,983
  • 4
  • 29
  • 49
  • Could you add the code of `Matters`? and the "_I'm making 5 calls to get all the results_"'s code? For now it's unclear if you fail to deserialize, looking for a way to concat multiple `list`, or just having a issue with the logic we do not see. – xdtTransform Jun 23 '20 at 09:52
  • What is relationship between `Companies` and `Matters` types? How do you make a call to get a json? – Pavel Anikhouski Jun 23 '20 at 09:52
  • Perhaps [Merge two List into one List in Linq](https://stackoverflow.com/questions/23016768/merge-two-listobject-into-one-list-in-linq), [Using LINQ to merge a list of objects](https://stackoverflow.com/questions/2298207/using-linq-to-merge-a-list-of-objects). It's unclear – xdtTransform Jun 23 '20 at 09:55
  • Matters should have been companies. Corrected now. – user2924019 Jun 23 '20 at 10:16
  • @xdtTransform I have put the code above. I don't fail to deserialize. I can deserialize fine, but I don't know how to combine the 5 pages of results into a single object. That's the part I'm struggling with. – user2924019 Jun 23 '20 at 10:26

1 Answers1

1

I think on solution would be to add the companies to a List:

var listOfCompanies = new List<Companies>();

Companies companies  = Clio.GetCompanies(5);
listOfCompanies.Add(compaines); 
do
{
    companies = Clio.GetCompanies(5, companies.Meta.Paging.Next.ToString());
    listOfCompanies.Add(compaines); 
} while (GetCompanies.Meta.Paging.Next != null);

foreach(var companyBatch in compaines)
{
     // process data here...
}

Or if you don't want to keep the metadata in the list:

var listOfCompanyData = new List<Datum>();

Companies companies  = Clio.GetCompanies(5);
listOfCompanyData.AddRange(compaines.data); 
do
{
    companies = Clio.GetCompanies(5, companies.Meta.Paging.Next.ToString());
    listOfCompanyData.AddRange(compaines.data); 
} while (GetCompanies.Meta.Paging.Next != null);

foreach(var companyDatum in listOfCompanyData)
{
     // process data here...
}
mortb
  • 9,361
  • 3
  • 26
  • 44
  • Thanks for this. I will have a look. Ultimately I just need the id and display_number in a list so I can later do a simple lookup to get the `id` by using the known `display_number`. The meta isn't important, only used to cycle through the multiple pages of JSON. – user2924019 Jun 23 '20 at 10:53
  • `new List();` should be `new List();` – user2924019 Jun 23 '20 at 12:50
  • Do you know how I can return the id's and the display_name in the foreach loop? I'm using `Debug.WriteLine(companyDatum));` which returns "Uploader.Datum[]". There aren't any id or display_name properties. – user2924019 Jun 23 '20 at 12:57