-
Nov 26th, 2021, 04:36 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Append response data to an object
I have recursive repeat of an requests to some REST API. And I need to append all outputs to my object.
Basically since limit is 50, I keep requesting new data until I reach end.
Code:
private void LoadOpenSeaEvents()
{
string Uri = "https://api.opensea.io/api/v1/events";
Uri += "?account_address=" + te_walletaddress.Text;
Uri += "&only_opensea=False";
Uri += "&event_type=successful";
Uri += "&limit=50";
Uri += "&offset={0}";
int offset = 0;
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Headers.Add("X-API-KEY", "SECURED");
string json_response = webClient.DownloadString(string.Format(Uri, offset));
var responseData = JsonConvert.DeserializeObject<Assets>(json_response);
var OpenSeaData = JsonConvert.DeserializeObject<Assets>(json_response);
List<Assets> ListOfTotal = new List<Assets>();
ListOfTotal.Add(OpenSeaData);
while (responseData.asset_events.Count() > 0)
{
offset += responseData.asset_events.Count();
json_response = webClient.DownloadString(string.Format(Uri, offset));
responseData = JsonConvert.DeserializeObject<Assets>(json_response);
OpenSeaData = JsonConvert.DeserializeObject<Assets>(json_response);
ListOfTotal.Add(OpenSeaData);
}
}
I tried almost everything that came to my mind but i just can't append two response data to same object.
Only way i found is to add output data to a ListOf objects, is there any way to avoid using ListOfTotal and have all data appended ?
Class available at https://pastebin.com/5ZjSEe9A
-
Nov 26th, 2021, 05:35 PM
#2
Re: Append response data to an object
Something like this, but everything really depends on the structure of asset…
Code:
while (responseData.asset_events.Count() > 0)
{
offset += responseData.asset_events.Count();
json_response = webClient.DownloadString(string.Format(Uri, offset));
responseData = JsonConvert.DeserializeObject<Assets>(json_response);
OpenSeaData &= JsonConvert.DeserializeObject<Assets>(json_response);
}
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 26th, 2021, 08:16 PM
#3
Re: Append response data to an object
Looking at the Assets class, it doesn’t make sense to append. Every one of those classes has a distinct ID and values. A List(of Assets) is the best option
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 27th, 2021, 11:50 AM
#4
Thread Starter
Addicted Member
Re: Append response data to an object
Originally Posted by .paul.
Looking at the Assets class, it doesn’t make sense to append. Every one of those classes has a distinct ID and values. A List(of Assets) is the best option
Thank you for your answer. With setting your first post suggestion I'm getting
Code:
Error CS0019 Operator '&=' cannot be applied to operands of type 'Assets' and 'Assets'
The other solution Idea is I should keep it the way as I have, in this case I have a list of responses, every item in list contains 50 Assets, wanted to have them inside one object so I can proceed with one for loop.
-
Nov 27th, 2021, 01:24 PM
#5
Thread Starter
Addicted Member
Re: Append response data to an object
Ok got it.
Thanks anyway
Code:
List<AssetEvent> AssetEvents = new List<AssetEvent>();
AssetEvents.AddRange(responseData.asset_events.ToList());
while (responseData.asset_events.Count() > 0)
{
offset += responseData.asset_events.Count();
json_response = webClient.DownloadString(string.Format(Uri, offset));
responseData = JsonConvert.DeserializeObject<Assets>(json_response);
AssetEvents.AddRange(responseData.asset_events.ToList());
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|