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);